-3

So I declare a multidimensional array of nodes like this

Node[,] nodes;

In order to store Node objects in it I have to do this

nodes = new Node[Xsize, Ysize];

but then when I go to store the actual Node objects I do this

for(int x = 0; x < Xsize; x++)
{
  for(int y = 0; y < Ysize; y++)
  {
     nodes[x,y] = new Node()
  }
}

My question is, why is step 2 of this necessary? I am already saying new Node() when I place my individual node objects in my multidimensional array, and it already knows the array is of type Node.

user3312266
  • 187
  • 1
  • 4
  • 14
  • 1
    You need to have somewhere to store those nodes before you create them. Your second code chunk instructs the compiler to create x amount of space which would later allow you to add only x amount of items to the array. – Deadzone Jul 16 '17 at 23:10
  • Short version: because that's how C# works. You want a new object of type `Node[,]`, you need to use `new`. Now you want to fill that 2d array with objects of type `Node`, you need `new` for each of those objects. Do note the special case: if `Node` is a value type (i.e. `struct`), an array of such objects is automatically initialized, with each instance stored in the array having the default values for the fields in that object. – Peter Duniho Jul 16 '17 at 23:34

2 Answers2

1

In step 2 (nodes = new Node[Xsize, Ysize];) you're initializing your Array. An Array must always have a size when being initialized, unlike a list.

In step 3 (nodes[x,y] = new Node()), you're initializing the object, and then place it at a certain spot in your array.

When doing step 3, you have to place the new Node within the initalized positions of your array. An example: if your step 2 would've been nodes = new Node[3], followed by nodes[3] = new Node();, you would get an OutOfBoundsException because nodes only has positions [0] [1] and [2] initialized.

CthenB
  • 800
  • 6
  • 17
  • What do you mean by "step 2" and "step 3"? Your answer will be clearer if you copy and paste the code from the OP which you refer to. – Code-Apprentice Jul 16 '17 at 23:10
  • "if you'd of said..." is not correct English. Try to find a way to reword this so it is grammatically correct and still expresses what you are trying to say. – Code-Apprentice Jul 16 '17 at 23:12
  • @Code-Apprentice he literally refers to initialization as 'step 2', you can easily infer initializing the objects is 'step 3' – CthenB Jul 16 '17 at 23:12
  • I missed the OP mentioning "step 2" when I skimmed the question. I now understand how you are referring to this. Still, your answer will be more clear and more useful if you are more explicit about which code you refer to. You seem to have the most accurate answer and I'm trying to help you improve it so that it will help future visitors as well as the OP. – Code-Apprentice Jul 16 '17 at 23:14
  • @Code-Apprentice see my edits – CthenB Jul 16 '17 at 23:22
0

Your first step is only declaring the variable and saying what type is can contain; A multidimensional array of Node objects, in this case. The second step is actually populating said variable with values. Because Node is a class, you have to use new in order to create instances of that class. Because the array know's it's type, it will allow Node objects to be stored in it.

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • What do you mean by "first step" and "second step"? Your answer will be clearer if you copy and paste the code from the OP which you refer to. – Code-Apprentice Jul 16 '17 at 23:10
  • 1
    @Code-Apprentice: Perhaps you should raise that complaint with the question, which talks about "step 2" without ever numbering them. Or perhaps the fact that both answers managed to figure out the numbering should be a clue that it isn't a problem at all. – Ben Voigt Jul 16 '17 at 23:12