?
Solved

Enumeration Problem.

Posted on 2004-04-07
4
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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