   a 2D grid with Multidimensional Arrays
  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 lineCell[ , ] 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]
  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.   ;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 thatQuote: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.   