Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enumeration Problem.

Posted on 2004-04-07
4
Medium Priority
?
279 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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