0

I'm trying to fill an array that is contained within a structure with some values but I keep getting errors no matter what I try.

my structure looks like this

public struct boardState
    {
        public int structid;
        public char[] state;
    }

bellow in the initializer I'm creating a new boardState and trying to fill it with some values like this

boardState _state_ = new boardState();
        _state_.structid = 1;
        _state_.state[9] = {'o','-','-','-','o','-','-','-','-','o'};

structid seems to work fine, but I get an error at the {'o','-' etc etc} telling me '; expected'. I've been through the code above and ensured that there are no ;'s missing (confirmed by the program running without this line) so I'm guessing you can't assign to the array in this way. How can I assign to the state array?

EDIT: - added the comma that I'd missed but still getting the same error.

E_S
  • 11
  • 3
  • 2
    Side note: please consider using class instead of struct. Or please confirm that you know what consequences of using struct in C# are (http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net) – Alexei Levenkov Apr 25 '12 at 23:54
  • 1
    I just have to say that I agree with Alexei, and add that you have a mutable struct which is generally considered a bad idea. Also, by having an object in the structure, most of the purpose of the struct is gone. – Guffa Apr 26 '12 at 00:09

2 Answers2

2

You're missing a comma and the syntax is off.

From:

_state_.state[9] = {'o','-','-','-','o','-','-','-','-''o'};

To:

_state_.state = new char [] {'o','-','-','-','o','-','-','-','-','o'};
Alex
  • 34,899
  • 5
  • 77
  • 90
  • Thanks for pointing that out. I've added that comma but I'm still getting the same error – E_S Apr 25 '12 at 23:53
2

You don't need [9]. It tries to assign an array to a single char. Instead just use this:

_state_.state = new char [] {'o','-','-','-','o','-','-','-','-','o'};
Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67