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
UnemployedNinja4mo 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
SG974mo ago
I'm a bit unsure on what you're asking
Pobiega
Pobiega4mo 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
Pobiega4mo 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
canton74mo 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
UnemployedNinja4mo 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
canton74mo ago
My way re-uses instances too (Not that that means much, as I'm using a `struct)
UnemployedNinja
UnemployedNinja4mo ago
Oh hm I think this is what I'm looking for, yes
Want results from more Discord servers?
Add your server
More Posts
Publishing/Building project with dependencies based on target .NET versionI have a project that requires an external library file, which I have different versions of, for dif✅ Index expressions are only supported with constants - Avalonia ReactiveUI```cs IObservable<bool> okUsername = (IObservable<bool>)this.WhenAnyValue( x => x.Username, x =>✅ Font Families Support By Avalonia?I have tried looking through the github repo and across google for a list of font families that the Asp.Net: Errors when changing server certificateI want to create a gRPC server in my local network and have to run it via SSL. For various reasons, C# .Net Core WebAPI Cyclic DependancyHello, trying to make simple web api, have two classes Character and Episode both classes has nav prHow to prevent drag outside of a given boundary .net Maui Android.I have been looking for a way to (as the title suggests) prevent a draggable control from going pastpython or c#i want learning for the next year python to create maybe my own ai and a chat's social network but iHangfire retries jobs at any serverI have ran into an issue with hangfire that causes my retry jobs to file due to them running on a seShould I learn WPF and Winforms first or should I learn MAUI since it's cross platform and newer?I want to branch into desktop application development. So I want someone with experience to let me kGenerate RSA keypairs with passphrase for SSH.NETI need to create a key pair which can be used by the renci.sshnet library to connect to a ssh server