Solved

Enumeration Problem.

Posted on 2004-04-07
4
272 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

11 Experts available now in Live!

Get 1:1 Help Now