Data with enums

I want to add some data with enums, but you can't do much more than bytes or ints in C#. To go around this, I've done this instead:
8 Replies
UnemployedNinja
UnemployedNinjaOP9mo ago
c#
public enum EServer {
Austrailia,
Canada,
Germany,
Spain,
France,
Ireland,
Netherlands,
NewZealand,
Norway,
Poland,
Finland,
Sweeden,
Turkey,
UnitedKingdom,
UnitedStates
}
c#
public enum EServer {
Austrailia,
Canada,
Germany,
Spain,
France,
Ireland,
Netherlands,
NewZealand,
Norway,
Poland,
Finland,
Sweeden,
Turkey,
UnitedKingdom,
UnitedStates
}
c#
public class Country {

private static readonly Dictionary<EServer, Country> COUNTRIES = new Dictionary<EServer, Country>() {
{EServer.Austrailia, new Country(EServer.Austrailia, "Austrailia", "AU")},
{EServer.Canada, new Country(EServer.Canada, "Canada", "CA")},
{EServer.Germany, new Country(EServer.Germany, "Germany", "DE")},
{EServer.Spain, new Country(EServer.Spain, "Spain", "ES")},
{EServer.France, new Country(EServer.France, "France", "FR")},
{EServer.Ireland, new Country(EServer.Ireland, "Ireland", "IE")},
{EServer.Netherlands, new Country(EServer.Netherlands, "Netherlands", "NL")},
{EServer.NewZealand, new Country(EServer.NewZealand, "New Zealand", "ZN")},
{EServer.Norway, new Country(EServer.Norway, "Norway", "NO")},
{EServer.Poland, new Country(EServer.Poland, "Poland", "PL")},
{EServer.Finland, new Country(EServer.Finland, "Finland", "FL")},
{EServer.Sweeden, new Country(EServer.Sweeden, "Sweeden", "SE")},
{EServer.Turkey, new Country(EServer.Turkey, "Turkey", "TR")},
{EServer.UnitedKingdom, new Country(EServer.UnitedKingdom, "United Kingdom", "GB")},
{EServer.UnitedStates, new Country(EServer.UnitedStates, "United States", "US")},
};

public readonly EServer Server;
public readonly string Name;
public readonly string Code;

private Country(EServer server, string name, string code) {
Server = server;
Name = name;
Code = code;
}

public static Country Austrailia { get { return COUNTRIES[EServer.Austrailia]; } }
public static Country Canada { get { return COUNTRIES[EServer.Canada]; } }
public static Country Germany { get { return COUNTRIES[EServer.Germany]; } }
public static Country Spain { get { return COUNTRIES[EServer.Spain]; } }
// ... char limit

}
c#
public class Country {

private static readonly Dictionary<EServer, Country> COUNTRIES = new Dictionary<EServer, Country>() {
{EServer.Austrailia, new Country(EServer.Austrailia, "Austrailia", "AU")},
{EServer.Canada, new Country(EServer.Canada, "Canada", "CA")},
{EServer.Germany, new Country(EServer.Germany, "Germany", "DE")},
{EServer.Spain, new Country(EServer.Spain, "Spain", "ES")},
{EServer.France, new Country(EServer.France, "France", "FR")},
{EServer.Ireland, new Country(EServer.Ireland, "Ireland", "IE")},
{EServer.Netherlands, new Country(EServer.Netherlands, "Netherlands", "NL")},
{EServer.NewZealand, new Country(EServer.NewZealand, "New Zealand", "ZN")},
{EServer.Norway, new Country(EServer.Norway, "Norway", "NO")},
{EServer.Poland, new Country(EServer.Poland, "Poland", "PL")},
{EServer.Finland, new Country(EServer.Finland, "Finland", "FL")},
{EServer.Sweeden, new Country(EServer.Sweeden, "Sweeden", "SE")},
{EServer.Turkey, new Country(EServer.Turkey, "Turkey", "TR")},
{EServer.UnitedKingdom, new Country(EServer.UnitedKingdom, "United Kingdom", "GB")},
{EServer.UnitedStates, new Country(EServer.UnitedStates, "United States", "US")},
};

public readonly EServer Server;
public readonly string Name;
public readonly string Code;

private Country(EServer server, string name, string code) {
Server = server;
Name = name;
Code = code;
}

public static Country Austrailia { get { return COUNTRIES[EServer.Austrailia]; } }
public static Country Canada { get { return COUNTRIES[EServer.Canada]; } }
public static Country Germany { get { return COUNTRIES[EServer.Germany]; } }
public static Country Spain { get { return COUNTRIES[EServer.Spain]; } }
// ... char limit

}
This just seems wrong. Is there a better way to do it?
SG97
SG979mo ago
I'm a bit unsure on what you're asking
Pobiega
Pobiega9mo ago
Seems they are trying to do rich enums. There are packages let you do something similar to this. Lemme see if I can find it ..
Pobiega
Pobiega9mo ago
GitHub
GitHub - ardalis/SmartEnum: A base class for quickly and easily cre...
A base class for quickly and easily creating strongly typed enum replacements in C#. - ardalis/SmartEnum
canton7
canton79mo ago
Do you need the dictionary there? The normal pattern is just:
public struct Country
{
// fields, ctor
public static Country Australia { get; } = new("Australia", "AU");
// ...
}
public struct Country
{
// fields, ctor
public static Country Australia { get; } = new("Australia", "AU");
// ...
}
UnemployedNinja
UnemployedNinjaOP9mo ago
I did it the way I did it so that I could recycle the same instance instead of creating a new one each time
canton7
canton79mo ago
My way re-uses instances too (Not that that means much, as I'm using a `struct)
UnemployedNinja
UnemployedNinjaOP9mo ago
Oh hm I think this is what I'm looking for, yes
Want results from more Discord servers?
Add your server