Jump to content
  • Advertisement
Sign in to follow this  
Hartley

a 2D grid with Multidimensional Arrays

This topic is 3495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright I'm relatively new to C#, I want to express a 2D Grid using an array, I figure it will be best to use a multidimensional array, each object in the array is of type Cell, which stores a position, for example, (2,1) anyways Im having trouble initializing my grid, I want to use a for loop like.
 public class Grid
    {
        Cell[ , ] cells;
        Vector2 tempCellPos = Vector2.Zero;

        public Grid(Vector2 size)
        {
            for (int x = 0; x < size.X; x++)
            {
                for (int y = 0; y < size.Y; y++)
                {
                    tempCellPos.X = (x * 32); tempCellPos.Y = (y * 32); //Making the grid with a multidimensional array
                    //Send the arguments to the constructor for each cell.
                    cells[x, y] = new Cell(tempCellPos, Cell.cellTypeGrass);
                }

            }
        }
    }
But im getting a problem, on this line Cell[ , ] cells; Says its never assigned to, and will always have its default value null. although I'm assigning it in the for loop. whats wrong? =( [Edited by - Hartley on May 19, 2009 3:13:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
You must initialize your cells when you declare them. Here are three different ways you can declare a 2D array.

int [ , ] arr1 = new int [ 4, 4 ] ;
arr1 = new int [ 4, 4 ] ;
int [ , ] arr2 = { { 3, 5, 7, 9 }, { 11, 13, 15, 17 } } ;

You probably want to use the first method, make sure you have a default constructor for your Cell class.

Then inside the for loop you can initialize each cell to the desired values.

Share this post


Link to post
Share on other sites
Quote:

Cell[ , ] cells;

Says its never assigned to, and will always have its default value null. although I'm assigning it in the for loop.

whats wrong? =(

The compiler is telling you that you never assign anything to cells. In the loop, you assign to cells[x, y] (which is not the same thing). You have to assign cells something, perhaps as shown in the above post, first. Otherwise you will get a null reference exception at runtime.

However, do note that
Quote:

You probably want to use the first method, make sure you have a default constructor for your Cell class.

You don't need a default constructor (unlike C++) and in fact it will have no impact on the result. In C# the elements of the array will be initialized to default(T), where T is the element type (Cell, in this case). For a reference type, default(T) is null, and for a value type default(T) is the built-in (noncustomizable) default constructed instance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!