Solved

Enumeration Problem.

Posted on 2004-04-07
4
273 Views
Last Modified: 2010-04-15
I have the following enumeration:

 namespace myCollection
{
      public enum collection1
     {
         red = 32,
         orange = 45
     }

    public enum collection2
    {
        red = 31,
        orange = 44
    }
}


 i have a constructor which takes an enum for construction of the object.

public myObject(Enum collection)
{
    // Here i need the name of the enum.
   // Which i am able to get like this.
   string strEnumName = collection.GetType().Name;
   
   // I also need the value for this enumeration.
   // for example the user passes in : myCollection.collection1.red

   // ????? How can i get the value of red for this enumeration.
   // here i should be able to get the value for red, which is: 32
   // PLEASE HELP.
}
0
Comment
Question by:steve_mick972
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10776816
you just need to convert to the actual enum then cast it

int iVal = (int)Convert.ChangeType(collection, collection.GetType());
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10779105

dfiala has the right idea, although it is a bit overkill


int myColor = (int) collection1.red;


0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10779122


...uh.. er, nevermind.  I needed to look a little more closely.  You are passing in a generic ENUM and you need to decipher which enum it is and THEN get the value.

dfiala has the best/working solution.  The suggestion I posted above is not sufficient.
0
 
LVL 1

Expert Comment

by:Troy_Lyne
ID: 10787427
Have you thought of using classes instead?

      public abstract class MyCollection
      {
            public abstract int red
            {
                get;
            }

            public abstract int orange
            {
                  get;
            }
      }

      public class MyCollection1 : MyCollection
      {
            public override int red
            {
                  get { return 1; }
            }      
      
            public override int orange
            {
                  get { return 2; }
            }
      }

      public class MyCollection2 : MyCollection
      {
            public override int red
            {
                  get { return 3; }
            }      
      
            public override int orange
            {
                  get { return 4; }
            }
      }

or a singleton pattern:

public class MyCollection
{
private static MyCollection mycollection1 = new MyCollection(1,2);
private static MyCollection mycollection2 = new MyCollection(3,4);

public static MyCollection MyCollection1
{
   get { return mycollection1; }
}

public static MyCollection MyCollection2
{
  get { return mycollection2; }
}


private int i_red;
private int i_orange;

public int red
{
  get { return i_red; }
}

public int orange
{
   get { return i_orange; }
}

private MyCollection(int red, int orange)
{
   this.i_red = red;
   this.i_orange = orange;
}
}


Somthing to think about:  You sould not be evaluating the numeric value of your enumeration.  Enumerations are more or less constants with the idea that another programmer could come along and change the value (red now = 15), but your application shouldn't care as red still equals red.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
free scanner TWAIN can be operated with a Web application 9 41
Device Location 4 19
C# guarantee sql connection close 6 32
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now