简体   繁体   中英

filling an array in a struct c#

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.

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'};

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'};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM