I'm obviously just missing something REALLY simple

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm sure once someone answers this I'll feel really dumb.

I'm a c++ programmer playing with c# an here's my conundrum

I declare a class in the for

public class Form1 : System.Windows.Forms.For


class Class

public int

ge

return x

se

x = value



}
..


I declare an array of the class in the body of the form as a member of the form

ClassA [] ClassAArray = new ClassA[10]

In the Consructor of the form I init the array (this is probably what I'm messing up

public Form1(

InitializeComponent()

for(int i = 0 ; i < 10 ; i++

ClassAArray= new ClassA()



When I try to access the member "i" it's not initialized, and i get a stack overflow exception

private void button1_Click(object sender, System.EventArgs e

ClassAArray[0].x = 0


Orinigally did this with a string member, but the same thing happens with and int member

Can anyone tell me what I'm missing about creating an instance of an array of a class

Thanks

Eric
 
Caladin said:
public int x
{
get
{
return x;
}
set
{
x = value;
}
}
When I try to access the member "i" it's not initialized, and i get a
stack overflow exception.

A stack overflow exception is precisely what I'd expect this code to throw;
it's an infinite loop. Here's a template that will solve your problem:

public class foo {
protected int _x;
public int x {
get { return _x; }
set { _x = value; }
}
}
 
Back
Top