x


Multidimensional array of structs

I'm trying to use a multidimensional array of structs in C#. After some time I have managed to write a single row array without fixed length. The code is as follows:

public GameObject block;
public GameObject emptySpace;

public struct cell {
    public int cellRotation;
    public GameObject cellBlock;

    public cell(int r, GameObject b){
        cellRotation = r;
        cellBlock = b;
    }
}

void Start () {
    cell[] map = {
        new cell(90, block), new cell(180, emptySpace), new cell(270, block)
    };
}

I can access the structs' properties easily by doing something like map[0].cellRotation

My question is, how can I define a new multidimensional array of structs of fixed length (say [2,3]) and how could I access it later on? Every single thing I've tried returns an error.

Thanks

more ▼

asked May 15 '12 at 02:13 PM

Agemoi gravatar image

Agemoi
17 3 3 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

C# documentation about multidimensional arrays: http://msdn.microsoft.com/en-us/library/2yd9wwz4%28v=vs.100%29.aspx

more ▼

answered May 15 '12 at 02:27 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

Thanks Nicolas Musset. I don't know why it refused to compile before, but I can write a second row now. It still returns an error when I try to specify its length, though.

May 15 '12 at 03:13 PM Agemoi

Could you show us the line that is causing the error?

May 15 '12 at 03:59 PM Kryptos
(comments are locked)
10|3000 characters needed characters left

Finally got it to work. I'm sorry for wasting your time. In case anyone else encounters a similar problem it looks like this:

cell[,] map = new cell[2, 3]{
    {new cell(90, block), new cell(180, emptySpace), new cell(270, block)},
    {new cell(90, emptySpace), new cell(180, block), new cell(270, emptySpace)}
};

You would have had to set the struct with the constructor inside first.

Thanks everyone

more ▼

answered May 15 '12 at 10:08 PM

Agemoi gravatar image

Agemoi
17 3 3 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4179
x1363
x48

asked: May 15 '12 at 02:13 PM

Seen: 1219 times

Last Updated: May 15 '12 at 10:08 PM