0

Well, I'm trying to create a struct inside another, and am having trouble ...

The code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WAMServer
{
    class PStruct
    {
        public static PStruct.Player[] player = new PStruct.Player[100];
        public struct Player
        {
            public int ID;
            public string Username;
            public string Password;
            public PStruct.Character[] character = new PStruct.Character[2];
        }

        public struct Character
        {
            public string CharacterName;
            public string Gender;
            public string ClassId;
            public string Level;
            public sbyte MapId;
            public int X;
            public int Y;
        }
    }
}

Uses the struct:

PStruct.player[index].character[Convert.ToInt32(ID)].CharacterName = br.ReadString();
PStruct.player[index].character[Convert.ToInt32(ID)].Gender = br.ReadString();
PStruct.player[index].character[Convert.ToInt32(ID)].ClassId = br.ReadString();
PStruct.player[index].character[Convert.ToInt32(ID)].Level = br.ReadString();

And:

string charName = (PStruct.player[clientId].character[Convert.ToInt32(charId)].CharacterName);

string charGender = (PStruct.player[clientId].character[Convert.ToInt32(charId)].Gender);

string charClass = (PStruct.player[clientId].character[Convert.ToInt32(charId)].ClassId);

string charLevel = (PStruct.player[clientId].character[Convert.ToInt32(charId)].Level);

The message I get is: Cannot have instance field initializers in struct

In the line:

public PStruct.Character[] character = new PStruct.Character[2];

Anyone can help me?

1
  • Does it have to be structs and arrays? Why not classes and Lists? Commented Apr 25, 2014 at 4:17

2 Answers 2

1

You cannot do this inside a struct.

public PStruct.Character[] character = new PStruct.Character[2]; //doesn't work

The new PStruct.Character[2]; is what the compiler has a problem with. That is, you're initializing the field inline. The only way to initialize a field in a struct is via an explicit constructor which takes parameters, as you also cannot have an explicit parameterless constructor in a struct, either.

public struct Player
{
    public Player() { } // doesn't work either - constructor must have parameters  
}

To accomplish what you want and keep it as a struct (and not have to pass a dummy parameter when instantiating the struct), the workaround is to use a good old-fashioned property with an explicit getter and setter:

public struct Player
{
    public int ID;
    public string Username;
    public string Password;

    private PStruct.Character[] character;
    public PStruct.Character[] Character 
    {
        get 
        { 
            if (null == character) 
                character = new PStruct.Character[2]; // works

            return character; 
        }
        set 
        { 
            character = value; 
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that a struct that mutates itself upon getting a property can behave in perhaps unexpected ways. Try here
0

Change your inner structs to class, and you'll be fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.