I got a two dimensional Array
BoardTile tiles[,];
and then in Init(Point size) I set its size:
tiles = new BoardTile[size.X, size.Y];
And how do I initialize all those elements because it does not use default BoardTile() constructor. It just assigns null.
foreach(BoardTile t in tiles) t = new BoardTile()
Does not work. And when I try to call
foreach(BoardTile t in tiles) t.anything()
I get NullReferenceException.
You can try nested loops:
for (int i = 0; i < titles.GetLength(0); ++i)
for (int j = 0; j < titles.GetLength(1); ++j)
titles[i, j] = new BoardTile();
Edit: if nested loops are too complex and unreadable, try switching to jagged arrays i.e. array of array - BoardTile tiles[][]; - from 2D one BoardTile tiles[,], e.g.
// created and initialized jagged array
BoardTile tiles[][] = Enumerable
.Range(size.Y) // size.Y lines
.Select(y => Enumerable // each line is
.Range(size.X) // size.X items
.Select(x => new BoardTile()) // each of them is BoardTile()
.ToArray()) // materialized as array
.ToArray(); // all arrays are array of array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With