C#
C#

help

Root Question Message

Populus
Populus8/21/2022
Paradoxical initializers [Answered]

Two classes. Class Character and Class Guild.
Character has a field of Guild.
Guild has an array of Members composed of a Character[] array.

Wondering how to re-concile the constructors to allow for creation of one before the other.

Code attached:
public class Character
    {
        public string? Name { get; set; } // In-game identifier.
        public Guild? Guild { get; set; } // In-game guild identifier.

        public Character(string Name)
        { 
            this.Name = Name; 
        }

        public Character(
            string Name,
            Guild Guild,
            )
        {
            this.Name = Name;
            this.Guild = Guild;
        }
    }

public class Guild
    {
        public string? Name { get; set; } // Name of Guild. (i.e "chumbucket & Associates").
        public Character[]? Members { get; set; } // Array of characters.

        public Guild(string Name, Character[] Members)
        {
            this.Name = Name;
            this.Members = Members;
        }
    }
Message Not Public

Sign In and Join Server To See

8/21/2022
Message Not Public

Sign In and Join Server To See

8/21/2022
Populus
Populus8/21/2022
So I can't pass it an empty object of Characters? I need to switch from object of Char to List of Char?
Doombox
Doombox8/21/2022
yeah, generally you don't use arrays unless you know exactly how many elements are going to be in there and have no need for dynamic changes, it's a lot harder to manage moving/removing entities from an array manually
Doombox
Doombox8/21/2022
generally a List or HashSet (if you need quick lookups and guaranteed uniqueness) is better for dynamic collections
Doombox
Doombox8/21/2022
an array would make more sense if you needed two things, a guaranteed limit on the number of members (which doesn't really need an array anyway) and some strict positioning rules (where a character is in the array actually means something)
Doombox
Doombox8/21/2022
but you can do both without an array anyway, so there's no real need
Populus
Populus8/21/2022
Alright, I'm just getting started. But I assume the keyword I should be researching is List<Collection>?
Populus
Populus8/21/2022
That was simple. Thanks a bunch for the help.
Message Not Public

Sign In and Join Server To See

8/21/2022
Populus
Populus8/21/2022
Right. I just remember that's how it was written for Java collections.
Populus
Populus8/21/2022
OQ is resolved. Do I mark this thread as complete or just let it sit and eventually be forgotten?
Message Not Public

Sign In and Join Server To See

8/21/2022
Message Not Public

Sign In and Join Server To See

8/21/2022
Doombox
Doombox8/21/2022
to the rescue
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy