Solved

Enumeration Problem.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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