|
OK thanks Pete. Sorry, it wasn't so easy to use that objects for me but now it's working fine. Do you have some other idea on how it would be better to implement them. What do you thinks about to use these classes with Lists ?
|
|
|
|
|
There's two problems here which I think come from the same misunderstanding.
In C, the code
struct A {
int someField;
struct B {
long somethingElse; }
} B;
}
... defines an object tree. That is, I can say
A a;
a.B.somethingElse = 12L;
In C#, you have not written the equivalent. Nested classes (and structs) define only the class, they do not define a field that goes with it. The fact that they're nested only affect name resolution and visibility. You need to also provide a field or property that is linked to an instance or array of the inner class.
That means I think you want:
public class product
{
public bool enabled;
public string productName;
public double zero;
public double span;
public string unit;
public preProcessing[] pp;
public class preProcessing
{
public string preProcessingName;
public double[] param = new double[5];
}
}
(Set aside for the moment questions of array vs list and public fields.) That means you can say
var p = new product();
p.pp = new product.preProcessing[10];
p.pp[0] = new product.preProcessing();
p.pp[0].param[3] = 12.8;
You've made the same mistake at the top level: Parameters should have a field of type product[].
When you do that you'll see the second mistake, because your code will stop compiling due to inconsistent [Serializable] attributes. You need to put [Serializable] on all the inner classes that are part of your data tree, too.
|
|
|
|
|
Thanks Bob you were, as usual, clear and helpful. Now the work is running in the right way!
|
|
|
|
|
Sorry, my application doesn't serialize/deserialize. How can I save all my classes together in the same file ? With only one simple class (Parameters) it was correct, now with more classes I don't know how to fix the code. The code to test the serailize and deserialize method now is:
private Parameters param = new Parameters();
private Parameters.product[] p = new Parameters.product[30];
private Parameters.preProcessing[] pp = new Parameters.preProcessing[10];
public MainForm()
{
param.InitParameters();
p[0] = new Parameters.product();
p[0].productName = "H2O";
p[0].preProcessId[0] = 0;
p[0].preProcessId[1] = 1;
param.SaveParameters(p[0]);
param.ReadParameters(out p);
pp[0] = new Parameters.preProcessing();
pp[0].preProcessId = 0;
pp[0].preProcessingName = "Derivative";
pp[0].preProcessingParms[0] = 10.0;
pp[0].preProcessingParms[1] = 20.0;
}
and the file with the classes to save and the serialize/deserialize methods is:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace My_Test_Space
{
[Serializable()]
public class Parameters
{
public int autoLockTime;
public int screenOffTime;
public int loopAutoStartTime;
[Serializable()]
public class product
{
public bool enabled;
public string productName;
public double zero;
public double span;
public string unit;
public int[] preProcessId = new int[10];
}
[Serializable()]
public class preProcessing
{
public int preProcessId;
public string preProcessingName;
public double[] preProcessingParms = new double[5];
}
public void InitParameters()
{
autoLockTime = 0;
screenOffTime = 0;
loopAutoStartTime = 0;
preProcessing[] pp = new preProcessing[10];
for (int i = 0; i < 9; i++)
{
pp[i] = new preProcessing();
pp[i].preProcessId = i;
pp[i].preProcessingName = "";
for (int j = 0; j < 5; j++) pp[i].preProcessingParms[j] = 0.0;
}
product[] p = new product[30];
for (int i = 0; i < 30; i++)
{
p[i] = new product();
p[i].enabled = false;
p[i].unit = "mm";
p[i].productName = "H2O";
p[i].zero = 0.0;
p[i].span = 0.0;
for (int j = 0; j < 10; j++)
{
p[i].preProcessId[j] = 0;
}
}
}
public void SaveParameters(Parameters obj)
{
Stream stream = File.Open("Parameters.bin", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
}
public void ReadParameters(out Parameters obj)
{
Stream stream = File.Open("Parameters.bin", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
obj = (Parameters)formatter.Deserialize(stream);
stream.Close();
}
}
}
|
|
|
|
|
You still haven't quite got the concept that an instance of Parameters should contain all references to instances of the subclasses. You're storing references to the inner classes in MainForm – that means they're not in the same object tree and really aren't related to the object 'param' you create.
Your MainForm initialisation should just be
private Parameters param = new Parameters();
Parameters.InitParameters should build the instances of products and products.preProcessing – not as local variables it is now. You're currently building arrays in that function that are never assigned to anything and go out of scope immediately.
public void InitParameters()
{
autoLockTime = 0;
screenOffTime = 0;
loopAutoStartTime = 0;
this.pp = new preProcessing[10];
for (int i = 0; i < 9; i++)
{
pp[i] = new preProcessing();
pp[i].preProcessId = i;
pp[i].preProcessingName = "";
for (int j = 0; j < 5; j++) pp[i].preProcessingParms[j] = 0.0;
}
this.p = new product[30];
for (int i = 0; i < 30; i++)
{
p[i] = new product();
p[i].enabled = false;
p[i].unit = "mm";
p[i].productName = "H2O";
p[i].zero = 0.0;
p[i].span = 0.0;
for (int j = 0; j < 10; j++)
{
p[i].preProcessId[j] = 0;
}
}
}
You'll have to add the declarations for p and pp to the Parameters class as I explained in my previous post to make those assignments work.
Now, all you have to do is serialise an instance of Parameters (i.e. MainForm.param), and it will serialise the whole object tree.
|
|
|
|
|
OK Bob, now it works! I'm sorry for my stupidity, but from the beginning the 3 classes (which in fact contain much more data) were divided into 3 different files with 3 different classes so I have created a lot of confusion to forget even the most basic concepts. I tried many solutions to solve the problem of the persistence of the data (w/o databases or registry) using settings at first, then with the structures, then with static classes (and all thre 3 worked very well) but at the end also to study more deeply the dynamic classes and the problem of their serialization now I am very happy for the results achieved with your help.
|
|
|
|
|
Dear all please provide code we have 2 role 1)admin 2)user but we have only single login page when we select admin role in login page page redirect to admin panel and when role select at login page redirect to user panel
Please provide code in C#
|
|
|
|
|
Member 10678352 wrote: Please provide code in C# If you need code samples then you should try searching for them.
|
|
|
|
|
i have already searching for code but i not did not found any code...
|
|
|
|
|
|
|
|
I am trying the following sql statement on a simple windows form textbox:
select count(*) from job_orders;
then I am running the code using the following:
when I use the dtandared DataGrid from Microsoft Visual studio I get the correct result (total_count) in the row
but when I use the same with DevExpress xtraGrid I just get blank row although other sql statements are working fine in xtraGrid such as:
Select * from job_orders;
here is the code:
sql_connection = new MySqlConnection("Server=127.0.0.1;Port=3306;Database=MyDB;Uid=root;Pwd=MyPwd;Connect Timeout=360;");
sql_connection.Open();
sql_command = new MySqlCommand(txtQuery.Text, sql_connection);
sql_command.CommandType = CommandType.Text;
sql_adapter = new MySqlDataAdapter(sql_command);
sql_adapter.Fill(data_table);
dataGridView1.DataSource = null;
dataGridView1.ResetBindings();
dataGridView1.DataSource = data_table;
Technology News @ www.JassimRahma.com
|
|
|
|
|
Hi ,
i am a iphone app developer, i have decided to move to asp.net mvc now .I have litele knowledge on VC++ and c# long back.
Is it a gud decission or not?
If yes how do i start.Can any one guide me plz?
modified 21-Mar-14 5:55am.
|
|
|
|
|
|
If you are truly moving to ASP.NET (which is web based) you do not even want to think about C++ - look at C# instead.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I have a Class called horse. Its fields are name; color; age; sex.
I would like to loop thru an arry of names like trigger; royal; crown; etc.
I would like to do this.
foreach (string name in arrayNames)
{
horse name = new horse();
}
Now I have a class for each horses name. Can this be done.
Thanks Ray
|
|
|
|
|
You're creating a horse object, but then forgetting about it and it's being destroyed.
You have to add each horse to a collection to keep it alive.
You also should probably set the name of each horse after you create each instance of horse.
|
|
|
|
|
You should really declare a collection of Horses, and then in your loop, create a new Horse and give a name based on the current name in the array
List<Horse> allHorses = new List<Horse>();
foreach(string name in arrayNames)
{
horses.Add(new Horse(){ Name = name };
}
Everyone dies - but not everyone lives
|
|
|
|
|
In addition to previous answers I would create a Dictionary [^] instead of a List (given the horsename is unique).
Dictionary<string, horse> horses = new Dictionary<string, horse>();
then in your loop you can do:
foreach (string name in arrayNames)
{
horse h = new horse();
horses.add(name, h);
}
later in code you can get the horse out of the collection using the name.
In addition I would also add the name property to the horse object itself.
Hope this helps.
|
|
|
|
|
Just to add to the previous answers: you can't do that anyway.
foreach (string name in arrayNames)
{
horse name = new horse();
}
Because the declaration of your new horse inside the loop "hides" the name of the string used in the foreach - which will give you a compilation error:
A local variable named 'name' cannot be declared in this scope because it would give a different meaning to 'name', which is already used in a 'parent or current' scope to denote something else
Instead, you probably want to call it "horse" or similar so that you can access both the horse instance you just created and the name you want to give it.
BTW: You should try to follow naming conventions as they make everything slightly more understandable. And one of the conventions is that class names start with an uppercase letter: "Horse" rather than "horse":
string[] arrayNames = new string[] { "Trigger", "Royal", "Crown" };
List<Horse> horses = new List<Horse>();
foreach (string name in arrayNames)
{
Horse horse = new Horse();
horse.Name = name;
horses.Add(horse);
}
...
public class Horse
{
public string Name { get; set; }
public int Age { get; set; }
public Color Colour { get; set; }
public string Sex { get; set; }
}
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Just to throw another thing in there, the most concise way to write this in modern C# is
var horses = arrayNames.Select(n => new horse { name = n }).ToList();
ToList is optional if you only ever want to enumerate over it, but it's probably what you want in this context.
|
|
|
|
|
I have an incoming json object that I can successfully deserialize into an object. However, I now need to send that object back to the UI and to do this we use Google Proto objects.
This is what the json string looks like coming in and what it should look like going back out:
"lang\":{\"en-US\":{\"name\":\"AS Test Assembly Activity\",\"description\":\"Activity to test assembly activities\"}
I am trying to organize my proto messages that would output the same but I am having no luck. Here is what I have so far.
Proto Messages:
message CustomActivityConfig {
optional CustomActivityLanguage lang = 5;
}
message CustomActivityLanguage {
required string key = 1;
repeated LanguageData lang = 2;
}
message LanguageData {
optional string name = 1;
optional string description = 2;
}
This gives me an output of:
"lang":{"key":"en-US","lang":[{"name":"AS Test Assembly Activity","description":"Activity to test assembly activities"}
My Class structure which holds the correct data from the json at the top looks like this:
[Serializable]
public class CustomAssemblyData
{
public Dictionary<string, LangData> Lang { get; set; }
}
[Serializable]
public class LangData
{
public string Name { get; set; }
public string Description { get; set; }
}
So what I am doing is iterating through the KeyValuePair in my class above and trying to pack it into my proto message.
I feel if I can get the proto right that it should work as expected
If this helps, here is how I am currently trying to pack the proto (which gives the second incorrect json output:
var cac = new CustomActivityConfig.Builder();
foreach (KeyValuePair<string, LangData> kvp in genericAssembly.CustomActivityConfig.Lang)
{
var culture = kvp.Key;
var name = kvp.Value.Name;
var desc = kvp.Value.Description;
var langData = new LanguageData.Builder()
{
Name = name,
Description = desc
}.Build();
var customActivityLanguage = new CustomActivityLanguage.Builder()
{
Key = culture
};
customActivityLanguage.AddLang(langData);
cac.Lang = customActivityLanguage.Build();
}
Also, if anyone can answer me this: Is there any way to get rid of the field names? 'Key', 'Lang' for instance and just show the values?
Hope this makes sense. Appreciate the time.
|
|
|
|
|
What are you actually using to build the protobuf? Are you using Jon Skeet's implementation[^]?
|
|
|
|
|
I believe that is what we are using but am not 100% on it. Reason being is that the implementation is a part of our framework here and we don't touch it but rather implement it. I cannot be certain how the core framework is using it.
I've determined that the 'dataType' so to speak is a Dictionary<string, class=""> and that will allow me to deserialize the json into an object correctly.
When it comes into my area it is a json, I deserialize into a class, now I need to pack it back into a proto and send it out in the exact same format it comes in at. I can't seem to order my proto in a way that does this.
modified 21-Mar-14 10:37am.
|
|
|
|