Arrays in a class definition
-
Hi ... I'm having trouble setting the values of the array in my class. I tried this ... and i can set/get "x.name" but pgm crashes with an error message that says i do not have an instance of the array item when i try to set "x.number[1]". namespace WindowsApplication1 { public partial class Form1 : Form { class myClass { // fields private string _name; private double[] _number; // properties public string name { get { return _name; } set { _name = value; } } public double[] number; public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } // Default constructor: public myClass() { name = ""; double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } // Clear Numbers public void ClearNumbers() { double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myClass x = new myClass(); x.name = "Charlie"; x.number[1] = 123.45; } } } My program works on x.name = "Charlie"; My program fails on x.number[1] = 123.45; Please advise on how to fix my class definition to accomodate arrays. Thank you. -- modified at 10:17 Saturday 25th March, 2006
-
Hi ... I'm having trouble setting the values of the array in my class. I tried this ... and i can set/get "x.name" but pgm crashes with an error message that says i do not have an instance of the array item when i try to set "x.number[1]". namespace WindowsApplication1 { public partial class Form1 : Form { class myClass { // fields private string _name; private double[] _number; // properties public string name { get { return _name; } set { _name = value; } } public double[] number; public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } // Default constructor: public myClass() { name = ""; double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } // Clear Numbers public void ClearNumbers() { double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myClass x = new myClass(); x.name = "Charlie"; x.number[1] = 123.45; } } } My program works on x.name = "Charlie"; My program fails on x.number[1] = 123.45; Please advise on how to fix my class definition to accomodate arrays. Thank you. -- modified at 10:17 Saturday 25th March, 2006
The code in the constructor is not even possible to compile. Why don't you show the code that you really are using? I assume that the code in the constructor looks like the code in the ClearNumbers method, as that at least compiles... You have two array references in the class, _number and number. You never assign anything to any of them, so of course you get an error message when you try to use any of them. If you look at the task list after compiling, you will see that the compiler has given you very informative warnings about this. --- b { font-weight: normal; }
-
The code in the constructor is not even possible to compile. Why don't you show the code that you really are using? I assume that the code in the constructor looks like the code in the ClearNumbers method, as that at least compiles... You have two array references in the class, _number and number. You never assign anything to any of them, so of course you get an error message when you try to use any of them. If you look at the task list after compiling, you will see that the compiler has given you very informative warnings about this. --- b { font-weight: normal; }
Sorry ... i have corrected the original post. i should have just copy/pasted the whole original .. i did not actually save that much space when i copied just part of the problem ... and i introduced an error. Thank you for pointing that out. As you can tell ... i am a newbie at class definitions. I have no idea why the underscore character precedes the field names and then (if needed at all) why it is not used in EVERY reference to the field. But not having a knowledgeable mentor ... i am subject to the problems associated with "mimicking" what i have seen in other class definitions. I think u get the idea of what i am trying to accomplsh. Please suggest code change to the class so the button code works. Thanks.
-
Sorry ... i have corrected the original post. i should have just copy/pasted the whole original .. i did not actually save that much space when i copied just part of the problem ... and i introduced an error. Thank you for pointing that out. As you can tell ... i am a newbie at class definitions. I have no idea why the underscore character precedes the field names and then (if needed at all) why it is not used in EVERY reference to the field. But not having a knowledgeable mentor ... i am subject to the problems associated with "mimicking" what i have seen in other class definitions. I think u get the idea of what i am trying to accomplsh. Please suggest code change to the class so the button code works. Thanks.
You have two arrays, like Guffa said,
number
and_number
and you dont assign anything to either of them. The fact that they have the same name, give or take an underscore, means nothing, they are completely different variables that are in no way related to each other. Your code crashes onx.number[1]
because you havent initialisednumber
to anything, and so it is currentlynull
. So trying to access the second value of it will crash because there is no second element. Try the following code, is should work...class myClass
{
// fields
private string _name;
private double[] _number = new double[5];// properties public string name { get { return \_name; } set { \_name = value; } } public double\[\] number { get { return \_number; } set { \_number = value; } } public double this\[int index\] { get { return \_number\[index\]; } set { \_number\[index\] = value; } } // Default constructor: public myClass() { name = ""; \_number\[0\] = 0; \_number\[1\] = 0; \_number\[2\] = 0; \_number\[3\] = 0; \_number\[4\] = 0; } // Clear Numbers public void ClearNumbers() { \_number\[0\] = 0; \_number\[1\] = 0; \_number\[2\] = 0; \_number\[3\] = 0; \_number\[4\] = 0; }
}
Hope that helps :) Cheers, Will H
-
You have two arrays, like Guffa said,
number
and_number
and you dont assign anything to either of them. The fact that they have the same name, give or take an underscore, means nothing, they are completely different variables that are in no way related to each other. Your code crashes onx.number[1]
because you havent initialisednumber
to anything, and so it is currentlynull
. So trying to access the second value of it will crash because there is no second element. Try the following code, is should work...class myClass
{
// fields
private string _name;
private double[] _number = new double[5];// properties public string name { get { return \_name; } set { \_name = value; } } public double\[\] number { get { return \_number; } set { \_number = value; } } public double this\[int index\] { get { return \_number\[index\]; } set { \_number\[index\] = value; } } // Default constructor: public myClass() { name = ""; \_number\[0\] = 0; \_number\[1\] = 0; \_number\[2\] = 0; \_number\[3\] = 0; \_number\[4\] = 0; } // Clear Numbers public void ClearNumbers() { \_number\[0\] = 0; \_number\[1\] = 0; \_number\[2\] = 0; \_number\[3\] = 0; \_number\[4\] = 0; }
}
Hope that helps :) Cheers, Will H
Thank you so VERY much, Will ! Your suggestions work great! I still do not understand the use of "this" in the code block ... public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } ... but for the time being, until i get that understanding, i can mimic what u showed me for future class definitions. Your help is much appreciated!
-
Thank you so VERY much, Will ! Your suggestions work great! I still do not understand the use of "this" in the code block ... public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } ... but for the time being, until i get that understanding, i can mimic what u showed me for future class definitions. Your help is much appreciated!
That code block is there so that you can access
myClass
as though it was an array itself. It means that if you create an instance ofmyClass
like somyClass foo = new myClass();
then you can access your
_number
array in two ways. Like this...double firstElement = foo.number[0];
and also like this...
double firstElement = foo[0];
The last way is only allowed because of the '
public double this[int index]
' accessor. Glad I could help :) Cheers, Will H