This is going to be an embarrassing question for me, but they say there is no worse question that one not asked. So I am maintaining someone else code in C#. Almost no problem there but there is something that is bothering me. There are several structs of two types:
private struct Struct1
{
public string name;
public int numX;
public int[] numA;
public int[,] x; //x coordinate
public int[,] y; //y coordinate
}
private struct struct2
{
public string name;
public double[] coA;
public double[] coB;
public double[] coC;
public double[] coD;
}
Struct1[] arrayOfS1;
Struct1 exOfS1;
Struct2[] arrayofS2;
Struct exOfS2;
They seem quite similar right. After that in the Form Load function we instantiate them
int k;
for(k=0;k<MAX_ENTRY;k++)
{
arrayOfS1[k].numA = new int[MAX_BOUNDARY];
arrayOfS1[k].x = new int[MAX_BOUNDARY, MAX_POINT];
arrayOfS1[k].y = new int[MAX_BOUNDARY, MAX_POINT];
}
exOfS1.numA = new int[MAX_BOUNDARY];
exOfS1.x = new int[MAX_BOUNDARY, MAX_POINT];
exOfS1.y = new int[MAX_BOUNDARY, MAX_POINT];
and something similar for the other struct. But my problem is:before that we do this:
arrayOfS1=new Struct1[MAX_ENTRY];
exOfS1= new Struct1(); //<------WHY THIS!!?
arrayOfS2= new Struct2[MAX_ENTRY];
//exOfS2=new Struct2(); //<----Why NOT this???
As you can see the array of structs are instantiated with new as they should be. In both cases. But for the variables that hold only one of the structs, only the first one (exOfS1) is instantiated with new, and not the second one. In any case, my question would be Do exOfS1 and exOfS2 need to be created with new??
In the code I have only one is created with new and I wonder why? Is it unnecessary?