I am developing an application in C# with winforms. I am pretty good with C++ but very new to C# so please forgive my ignorance.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class obj
{
private int member;
public obj(int n)
{ member = n; }
}
obj[] obj_arr = new obj[10];
obj_arr[0] = new obj(4); // Problem Here
}
}
This is a very simplified example of what I am trying to do, but as you can see I would like to declare an array of user defined objects. The problem I am having is that when I try to initialize the individual array member the compiler gives me an error. Actually it gives several errors. obj_arr[0] is highlighted with an error saying that it is a field but is being used as a type. The = is also highlighted with an error that says = is invalid token in class, struct, or interface declaration. Finally obj(4) is highlighted with an error saying method must have a return type.
I am a little stumped here, any help would be greatly appreciated.
New Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
obj_arr[0] = new obj(4); // Problem Here
}
class obj
{
private int member;
public obj(int n)
{ member = n; }
}
obj[] obj_arr = new obj[10];
obj o1 = obj_arr[0];
}