[2.0] creating an array of 2D arrays O_O
I need to create an array of 2D arrays, how would I do this?
int myArray1[,] = new int[,] {{1}, {2}};
int myArray2[,] = new int[,] {{3}, {5}};
int myPointer[] = new int[] {myArray1, myArray2};
then be able to reference myArray1 & 2, and it's values...
I have tried this:
Code:
public int[,] grid_1 = new int[,]
{{0, 0, 0, 1, 0, 0, 0, 1, 0, 1,1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1,0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 2,0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 2,0},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 2, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 2, 1, 1, 0, 0,0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0,0}};
public Point[] gridChangePoints = new Point[]
{new Point(0, 4)};
public int[] gridList = new int[] { grid_1 }; //line 37
The error I get is:
Code:
A field initializer cannot reference the nonstatic field, method, or property '_D_Game.Form1.Grids.Havana.grid_1' Line 37
Code:
Cannot implicitly convert type 'int[*,*]' to 'int' Line 37
I do not understand what it means by this? does anyone know? and does anyone know the solution?
Thanks
Re: [2.0] creating an array of 2D arrays O_O
You're declaring an array of int and then trying to initialise it with two 2D arrays. If you want your array to contain 2D arrays of int then thats what you have to specify its type as:
Code:
int[,] myArray1 = new int[,] {{1}, {2}};
int[,] myArray2 = new int[,] {{3}, {5}};
int[][,] myPointer = new int[][,] {myArray1, myArray2};
This part:specifies the type as array of 2D array of int.
Re: [2.0] creating an array of 2D arrays O_O
Ok, thanks, I left out a small amount of code :P which relates to the first error: but thanks you cleared the other one:D
Code:
Code:
public class Grids
{
public class Havana
{
public int[,] grid_1 = new int[,]
{{2, 2, 2, 2, 2, 0, 0, 1, 0, 1,1},
{0, 0, 0, 0, 2, 0, 0, 0, 0, 1,0},
{0, 0, 0, 0, 2, 0, 0, 0, 0, 2,0},
{0, 0, 0, 0, 2, 0, 0, 0, 1, 2,0},
{4, 0, 0, 0, 2, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 2, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 2, 1, 1, 0, 0,0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0,0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0,0}};
public Point[] gridChangePoints = new Point[]
{new Point(0, 4)};
public int[][,] gridList = new int[][,] { grid_1 };
}
public Havana havana = new Havana();
}
public Grids grids = new Grids();
The error occurs because I can not reference anything inside my class...
the error is the first error posted in my first post...
how can I fix this?
Thanks
Re: [2.0] creating an array of 2D arrays O_O
Hmmm... didn't really read the error message too carefully as I could see "the" issue straight away. I guess it was actually just "an" issue. :)
The gist of that error message is that you cannot initialise one member variable using another member variable. For instance, you can't do this:
CSharp Code:
public class SomeClass
{
private int x = 1;
private int y = x;
// ...
}
In that case you'd have to initialise the 'y' variable at least in the constructor instead of in the declaration:
CSHarp Code:
public class SomeClass
{
private int x = 1;
private int y;
public SomeClass()
{
this.y = this.x;
}
// ...
}