Correction: How to define an array in a class
-
Hi ... I corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.
-
Hi ... I corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.
Hi Your first problem is this line:
public double[] number;
You are declaring a public field called number. What you want is:
public double[] number { get { return _number; } }
You have made another mistake twice - in your constructor and your
ClearNumbers
method. This line declares a local variable called number:double[] number = new double[5];
What you want is:
_number = new double[5];
---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.
-
Hi Your first problem is this line:
public double[] number;
You are declaring a public field called number. What you want is:
public double[] number { get { return _number; } }
You have made another mistake twice - in your constructor and your
ClearNumbers
method. This line declares a local variable called number:double[] number = new double[5];
What you want is:
_number = new double[5];
---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.
Thank you VERY VERY VERY much ... it works great! Now i know how to do it in my real application. Once again, thanks for your help!
-
Hi ... I corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.