Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
enum network
        {
            AIRTEL  = 0701,
            AIRTEL  = 0708,
            AIRTEL  = 0802,
            AIRTEL  = 0808,
            AIRTEL  = 0812,
            ETISALAT = 0809,
            ETISALAT = 0817,
            ETISALAT = 0818,
            ETISALAT = 0909,
            GLO = 0705,
            GLO = 0805,
            GLO = 0807,
            GLO = 0811,
            GLO = 0815,
            MTN = 0703,
            MTN = 0706,
            MTN = 0803,
            MTN = 0806,
            MTN = 0810,
            MTN = 0813,
            MTN = 0814,
            MTN = 0816,
            MTN = 0903
        }


How can i check the constants against the varying values
Posted
Comments
phil.o 19-Dec-13 8:45am    
You cannot have several values in an enum with the same name.
The code you showed won't even compile.
Baba_Dee 19-Dec-13 8:49am    
I know that too, but can you help figure out other solution to do something like this instead of using enum
phil.o 19-Dec-13 9:20am    
Like what? You forgot to tell what is your goal.
Moreover, there is no solution for any object (whether it is a struct, a class or an enum) to hold several fields/properties/methods with the same name.
Baba_Dee 19-Dec-13 9:28am    
Thanks Boss.
Matt T Heffron 23-Dec-13 23:00pm    
Does my Solution 4 help?

You do realize your Enum example, "network," won't compile ? Each Enum entry is a named Constant which must be unique, and which must be an integral value type: int, byte, long, etc.

This will compile:
C#
enum network
{
    Airtel,
    ETISALAT,
    GLO,
    MTN
}
And, since an initializing value is not specified, or a Type, the Enum entries underlying Type is int, and they have a corresponding numeric value starting at the default, #0, and incrementing by #1. This is equivalent to:
C#
public static class NetWork
{
    public const int Airtel = 0;
    public const int ETISALAT = 1;
    public const int GLO = 2;
    public const int MTN = 3;
}
In your case you have one or more values you wish to associate with each Enum in 'network, and these are not in any order where you could express which values are associated with a specific Enum. That suggests this may not be a scenario in which an Enum is that useful.

There are many uses for Enums. I'd suggest you start by studying these articles:

MSDN: [^], [^].

CodeProject: [^], [^], [^].

Please describe with more detail what you mean by "check the constants against the varying values."
 
Share this answer
 
Comments
Karthik_Mahalingam 19-Dec-13 10:25am    
neat explanation. good
Try:
C#
network net = network.AIRTEL;
...
if (net == network.AIRTEL)
   {
   ...
   }
...
switch (net)
   {
   case network.AIRTEL:
   case network.ETISALAT:
      break;
   }
 
Share this answer
 
Comments
Ashok Nalam 19-Dec-13 8:49am    
you can not access network.AIRTEL as defined more than once.
OK, based on your: "How can i check the constants against the varying values" statement, I'm going to guess that you want to take these constant values and associate them to different categories represented by the enum.
You could try something like:
C#
enum network
{
    Unknown = 0,    // it is usually a good idea to define some "Unknown" or "None" identifier to value == 0
    Airtel,
    ETISALAT,
    GLO,
    MTN
}
static readonly Dictionary<int, network> NetworkCodeToNetworkMap =
  new Dictionary<int, network>(){
             { 0701, network.AIRTEL },
             { 0708, network.AIRTEL },
             { 0802, network.AIRTEL },
             { 0808, network.AIRTEL },
             { 0812, network.AIRTEL },
             { 0809, network.ETISALAT },
             { 0817, network.ETISALAT },
             { 0818, network.ETISALAT },
             { 0909, network.ETISALAT },
             { 0705, network.GLO },
             { 0805, network.GLO },
             { 0807, network.GLO },
             { 0811, network.GLO },
             { 0815, network.GLO },
             { 0703, network.MTN },
             { 0706, network.MTN },
             { 0803, network.MTN },
             { 0806, network.MTN },
             { 0810, network.MTN },
             { 0813, network.MTN },
             { 0814, network.MTN },
             { 0816, network.MTN },
             { 0903  network.MTN }};

Then to associate the number to the enum:
C#
network AssociateToNetwork(int code)
{
  network association;
  if (NetworkCodeToNetworkMap.TryGetValue(code, out association))
  {
    //association will be set to the "correct" value as defined in the Dictionary
    return association;
  }
  // you can report an error/warning here that the code was unknown
  // association will already be set to the 0 value here
  return association;
}  
 
Share this answer
 
You can define the enum like what you defined but while accessing values like network.AIRTEL you can not do because it says "This member is defined is more than once". Try to give Enum member names as unique and in order to read its value refer below url.

http://dotnetmirror.com/Articles/csharp/120/read-int-value-from-enum-csharp.net-example[^]
 
Share this answer
 
Comments
BillWoodruff 19-Dec-13 9:11am    
No, an Enum cannot be defined in the way the OP shows here. It will not compile.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900