I am looking for a best practice on how to create enum-like class that instead of numbers contains string values. Something like this:
public static class CustomerType
{
public static string Type1 = "Customer Type 1";
public static string Type2 = "Customer Type 2";
}
I would use this class throughout application as a value for all cases where I need CustomerType. I cannot use Enum because this is legacy system, and values like this are hardcoded everywhere, I am just trying to centralize them in one place.
Question is, in above example, should I use for declaring a variable:
What would be a best practice to set these kinds of classes and values?
If you are using C#, Why not create an enum and set string
based Description
attribute
for the enum
values as below:
public enum CustomerType
{
[System.ComponentModel.Description("Customer Type 1")]
Type1,
[System.ComponentModelDescription("Customer Type 2")]
Type2
}
Then, you can get the Description
value of enum values as below:
int value = CustermType.Type1;
string type1Description = Enums.GetDescription((CustomerType)value);
For various other ways to get the Description
attribute value of the enum, please refer this SO QA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With