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?

pravprab
  • 2,301
  • 3
  • 26
  • 43
user3571412
  • 105
  • 1
  • 9

2 Answers2

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; 
        }
    }
}
Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
  • Be aware that a struct that mutates itself upon getting a property can behave in perhaps unexpected ways. Try [here](https://tio.run/##fZJRS8MwEMff8ymOPrWInfponSATRJgwEBGUPWTpuQWzdFyuHXXss9e067pOp/eU3O@fu38uUe5cZYRVlTtt5/BcOsZlIvq7eJQZg4p1Zl38gBZJq0SIVT4zWoFjyhXDxMgSSWwE@GiRtgyP90k/5dV14ReHZOUST7GJdG6dUepbNJB0IRlb@j4FtZAkFSOdOOzxaI@hwTtDdcyRodtsDss69AeENjcGhsND/ehYU0fHYAgW1/uuV9MEBgPwrj@dODpEyDnZnudDzW23cv8463cspMnxV4Wt2ArhWLKfgjJ@eDChbE5y2T5Gi4pMp/AktQ2jH4MpJIHR7Y3G2vHN7jFvwyjpREbHd2ka1pIdDaMj@n4xjbvR@40vF0izWsjgb9Vlo5oh90Uj/8syg/EracaxthgGb0gZL0ASyRLQMpX@a8HXLrtq3NQJfwftriGAsxN@WrN@WFX1DQ) – Jeppe Stig Nielsen Apr 17 '21 at 19:14
0

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

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79