0

What I'm trying to do is define my own type that contains 2 ints 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 ints 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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.