TypeInitializationException with 2D array

F

Fencer

Hello, if I want to create 2D-array of ints I can do:

int[,] ints = {{1, 2, 3}, {4, 5, 6}};

However, when I try the same thing for the type Tile, which is a class
I've written:
Tile[,] tiles = {
{new Tile(), new Tile(), new Tile(), new Tile()},
{new Tile(), new Tile(), new Tile(Tile.TileType.WALL), new Tile()},
{new Tile(Tile.TileType.START), new Tile(Tile.TileType.WALL),
new Tile(), new Tile(Tile.TileType.END)}};

I get a TypeInitializationException. I placed a breakpoint at the first
line in the two Tile constructors, but it's not reached before I get
the unhandled exception. How can I make it work?

- Fencer
 
F

Family Tree Mike

Fencer said:
Hello, if I want to create 2D-array of ints I can do:

int[,] ints = {{1, 2, 3}, {4, 5, 6}};

However, when I try the same thing for the type Tile, which is a class
I've written:
Tile[,] tiles = {
{new Tile(), new Tile(), new Tile(), new Tile()},
{new Tile(), new Tile(), new Tile(Tile.TileType.WALL), new Tile()},
{new Tile(Tile.TileType.START), new Tile(Tile.TileType.WALL),
new Tile(), new Tile(Tile.TileType.END)}};

I get a TypeInitializationException. I placed a breakpoint at the first
line in the two Tile constructors, but it's not reached before I get
the unhandled exception. How can I make it work?

- Fencer

If the class Tile inherits from a base class, that base class
constructor will be called prior to the Tile constructors. Do you have
a base class and have you checked its constructor in the debugger?
 
F

Fencer

Family said:
If the class Tile inherits from a base class, that base class
constructor will be called prior to the Tile constructors. Do you have
a base class and have you checked its constructor in the debugger?

Thanks, you got me looking in the right direction. The Tile class did
indeed inherit from another class but the problem turned out to be
something else which I noticed when I investigated this. I thought the
problem was due to my 2D-array, I should have tried allocating just a
single Tile object!

- Fencer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top