Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign multiple values to enum elements

Tags:

c#

enums

Hi I have this enum currently

[Serializable]
public enum Country
{
    US      = 1,
    Canada  = 2,
}

When I usually get the integer from the database I convert it to the enum using

(Country) Convert.ToInt32("1")

I now have 2 sub regions in US and Canada, 1 & 2 for US and 3&4 for canada. So when I do

(Country) Convert.ToInt32("1") or (Country) Convert.ToInt32("2") i should get the enum to be US. and for 3 & 4 Canada. How do i implement this?

[Serializable]
public enum Country
{
    US      = 1,2
    Canada  = 3,4
}

Something like this. This is probably not right but just to give you an Idea.

like image 293
tHeSiD Avatar asked Dec 21 '10 19:12

tHeSiD


People also ask

How do I assign multiple values to an enum?

Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.

Can enum store multiple values?

The answer is no. You cannot store more that one value in an ENUM column.

Can you assign values to enums?

You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

Can enum have two same values?

1. Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.


2 Answers

An enum may not be the right construct to model this kind of problem.

I would suggest creating a class to represent country information, and provide methods the convert to and from numeric representations. With problems like this, you also have to decide what coding value you will use when converting a selected Country instance into a numeric value.

The Enum Object pattern can be helpful starting point for modeling this kind of situation:

public sealed class Country
{
    // initialize appropriately in the constructor...
    private readonly int[] m_Values;
    private readonly string m_Name;

    // make the constructor private so that only this class can set up instances
    private Country( string name, int[] codes ) { ... }

    public static Country US = new Country("United States", new[]{ 1,2 } );
    public static Country Canada = new Country("Canada", new[] {3,4} );

    public static Country FromCode( int code ) { ... }
    public override string ToString() { return m_Name; }
    // ... etc...
}

Based on your example, you should also consider whether you need to model Country sub-regions as first-class entities, rather than simply folding them into the implementation details of your Country enumeration. Whether you should do this or not depends on your requirements and use cases, so only you can make an appropriate decision on that.

like image 111
LBushkin Avatar answered Oct 18 '22 05:10

LBushkin


This is not possible. You'd have to use seperate values then. If the names are the same, ie.

[Serializable]
[Flags]
public enum Country
{
    US      = 1,
    Canada  = 2,
    Northern = 4,
    Southern = 8
}

You could do this: Countries = Country.US | Country.Northern. If not, you need to find another way, possible another property or even better, a Location class.

like image 44
Femaref Avatar answered Oct 18 '22 05:10

Femaref