C
C#3y ago
populus

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 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;
}
}
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;
}
}
9 Replies
Up
Up3y ago
how about you use a collection (such as a list) of characters for the guild? then just create the guild with an empty list (== no members), and add to it as needed you can technically do the same by simply replacing the array object as needed but it's not as clean as using collections that were designed with mutability in mind
populus
populusOP3y ago
So I can't pass it an empty object of Characters? I need to switch from object of Char to List of Char?
Doombox
Doombox3y ago
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 generally a List or HashSet (if you need quick lookups and guaranteed uniqueness) is better for dynamic collections 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) but you can do both without an array anyway, so there's no real need
populus
populusOP3y ago
Alright, I'm just getting started. But I assume the keyword I should be researching is List<Collection>? That was simple. Thanks a bunch for the help.
Up
Up3y ago
Well you don't want a list of collections, do you LUL
populus
populusOP3y ago
Right. I just remember that's how it was written for Java collections. OQ is resolved. Do I mark this thread as complete or just let it sit and eventually be forgotten?
Up
Up3y ago
Also no. It's the exact same in java. List<ThingInList>. If you make a List<Collection> that's a list of collections (of other things..) /close
Doombox
Doombox3y ago
to the rescue
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server