Simon Verbeke
Simon Verbeke

Reputation: 3005

Array of structs returns errors

I have the following code:

static int gridX = 40;
static int gridY = 40;

public struct CubeStruct
{
    public Transform cube;
    public bool alive;
    public Color color;
}

public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];

This returns the following errors:

error CS1519: Unexpected symbol `,' in class, struct, or interface member declaration

error CS0178: Invalid rank specifier: expected ,' or]'

error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration

It's probably something obvious, but I can't see it.

Upvotes: 3

Views: 782

Answers (4)

Adam
Adam

Reputation: 15803

In C#, nothing can float around outside of a Type. So you need to do this:
Also note that the [,] comes after the type, not after the identifier.

public class GridMain
{
    static int gridX = 40;
    static int gridY = 40;
    public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

}

public struct CubeStruct
{
    public Transform cube;
    public bool alive;
    public Color color;
}

Upvotes: 3

Roee Gavirel
Roee Gavirel

Reputation: 19443

change:

public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];

to:

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

Upvotes: 2

TehBoyan
TehBoyan

Reputation: 6890

public CubeStruct cubeArray[,] = new CubeStruct[gridX, gridY];

should be:

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726629

In C#, the [,] go before the name of the variable (i.e. it is not like in C/C++).

public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

Upvotes: 5

Related Questions