null value?
-
public class Command { public int Number; public int NoOfParameters; } Command[] Querries = new Command[50]; private void button_Click(object sender, EventArgs e) { initialization() Device mydevice = new Device(); int Add = mydevice.FindAddress(); } public int FindAddress() { SendCommand (Querries[0]) } My initialization method already initialize all Querries[i].Number and Querries[i].NoOfParameters. But when the code is run to SendCommand (Querries[0]), the values of Querries[i] is still null? Why is it so, and how to solve it ? Thanks
-
public class Command { public int Number; public int NoOfParameters; } Command[] Querries = new Command[50]; private void button_Click(object sender, EventArgs e) { initialization() Device mydevice = new Device(); int Add = mydevice.FindAddress(); } public int FindAddress() { SendCommand (Querries[0]) } My initialization method already initialize all Querries[i].Number and Querries[i].NoOfParameters. But when the code is run to SendCommand (Querries[0]), the values of Querries[i] is still null? Why is it so, and how to solve it ? Thanks
Hi, how does your initialization()-method look like? Maybe you could post the code, this could help us to find the problem. Regards Sebastian
It's not a bug, it's a feature! Me in Softwareland.
-
public class Command { public int Number; public int NoOfParameters; } Command[] Querries = new Command[50]; private void button_Click(object sender, EventArgs e) { initialization() Device mydevice = new Device(); int Add = mydevice.FindAddress(); } public int FindAddress() { SendCommand (Querries[0]) } My initialization method already initialize all Querries[i].Number and Querries[i].NoOfParameters. But when the code is run to SendCommand (Querries[0]), the values of Querries[i] is still null? Why is it so, and how to solve it ? Thanks
Can you show the Initialization() method ?
Jean-Christophe Grégoire
-
public class Command { public int Number; public int NoOfParameters; } Command[] Querries = new Command[50]; private void button_Click(object sender, EventArgs e) { initialization() Device mydevice = new Device(); int Add = mydevice.FindAddress(); } public int FindAddress() { SendCommand (Querries[0]) } My initialization method already initialize all Querries[i].Number and Querries[i].NoOfParameters. But when the code is run to SendCommand (Querries[0]), the values of Querries[i] is still null? Why is it so, and how to solve it ? Thanks
There must be dozens of your classmates that have already asked this question here. Just because you make the space for 50 instances of your object, doesn't at all mean that you've instantiated the object 50 times. For christ's sake, read your freakin' text books.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Can you show the Initialization() method ?
Jean-Christophe Grégoire
private void initialization() { Command[] Querries = new HARTCommand[50]; XmlDocument Reader = new XmlDocument(); Reader.Load("Myfile.xml"); XmlNodeList List = Reader.SelectNodes("/Common/Command"); foreach (XmlNode commandnode in List) { int i = int.Parse(commandnode.Attributes["value"].InnerText); int type= int.Parse(commandnode.Attributes["Para"].InnerTect); Querries[i] = new Command(); Querries[i].CommandNumber = i; Querries[i].NoOfParameters= type; } } I run the code ,and the initialization is working fine.
-
private void initialization() { Command[] Querries = new HARTCommand[50]; XmlDocument Reader = new XmlDocument(); Reader.Load("Myfile.xml"); XmlNodeList List = Reader.SelectNodes("/Common/Command"); foreach (XmlNode commandnode in List) { int i = int.Parse(commandnode.Attributes["value"].InnerText); int type= int.Parse(commandnode.Attributes["Para"].InnerTect); Querries[i] = new Command(); Querries[i].CommandNumber = i; Querries[i].NoOfParameters= type; } } I run the code ,and the initialization is working fine.
sorry, corrected one is here private void initialization() { Command[] Querries = new Command[50]; XmlDocument Reader = new XmlDocument(); Reader.Load("Myfile.xml"); XmlNodeList List = Reader.SelectNodes("/Common/Command"); foreach (XmlNode commandnode in List) { int i = int.Parse(commandnode.Attributes["value"].InnerText); int type= int.Parse(commandnode.Attributes["Para"].InnerTect); Querries[i] = new Command(); Querries[i].CommandNumber = i; Querries[i].NoOfParameters= type; } } I run the code ,and the initialization is working fine.
-
sorry, corrected one is here private void initialization() { Command[] Querries = new Command[50]; XmlDocument Reader = new XmlDocument(); Reader.Load("Myfile.xml"); XmlNodeList List = Reader.SelectNodes("/Common/Command"); foreach (XmlNode commandnode in List) { int i = int.Parse(commandnode.Attributes["value"].InnerText); int type= int.Parse(commandnode.Attributes["Para"].InnerTect); Querries[i] = new Command(); Querries[i].CommandNumber = i; Querries[i].NoOfParameters= type; } } I run the code ,and the initialization is working fine.
-
There must be dozens of your classmates that have already asked this question here. Just because you make the space for 50 instances of your object, doesn't at all mean that you've instantiated the object 50 times. For christ's sake, read your freakin' text books.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Do not be mad. In fact, the real problem is that the var he defined in Initializer method override the class field. :) Maybe the code is not clear enough.
Tan Li I Love KongFu~
-
The problem you have is because "Querries" is defined as a class-level property, but you're redefining it as a local variable in the initialization method. This locval copy is the one you're initializing, not the class-level one you go on to try to use.
-
The problem you have is because "Querries" is defined as a class-level property, but you're redefining it as a local variable in the initialization method. This locval copy is the one you're initializing, not the class-level one you go on to try to use.