Reputation: 73
What I'm trying to do is define my own type that contains 2 int
s to use in a 2-dimensional array. The application for this is using the array indexes as x,y coordinates for objects in 2-d space to be displayed. So object with data stored at array[13,5]
would be displayed at x=13,y=5, and the properties of that object could be retrieved with array[13,5].property1
, for example. The type I've defined is extremely simple:
chunkBlocks.cs:
public class chunkBlocks {
public int blockType;
public int isLoaded;
}
then, I initialize the array:
chunkBlocks[,] _bData = new chunkBlocks[17, 17];
This all compiles/runs without error. The NRE is thrown when I try to assign a value to one of the properties of the type. For debugging, I have the code written as:
_bData[i, n].blockType = 5;
and the NRE is thrown specifically on the .blockType
portion. I've tried changing the type to initialize with 0 values for both int
s to no avail:
public class chunkBlocks {
public int blockType = 0;
public int isLoaded = 0;
}
I've Googled around and searched SO, and I've not been able to turn up anything. I'm sure it's a relatively simple matter, but I'm not experienced enough to be able to pinpoint it.
Thanks!
Upvotes: 0
Views: 51
Reputation: 1036
I think you should do this:
for(int i = 0;i<17;i++)
{
for (int j = 0; j < 17; j++)
{
_bData[i, j] = new chunkBlocks ();
}
}
Upvotes: 0
Reputation: 12449
You need to initialize every instance of the array:
_bData[i, n] = new chunkBlocks();
Now assign the value to it:
_bData[i, n].blockType = 5;
You will have to initialize every instance, you just have declared them in the array.
Upvotes: 4