Solved

Alias for enum values

Posted on 2006-06-27
9
3,059 Views
Last Modified: 2012-05-05
Is it possible to alias enum values?

Someone here showed me this as part of another question, where for example "Daily" and "DY" should map to the same value:

public enum TimePeriod
  {
    Unknown,
    Daily,
    Weekly,
    Biweekly,
    Monthly,
    Yearly,
    AbbrevStart,
    Unk = Unknown,
    DY = Daily,
    WK = Weekly,
    BW = Biweekly,
    MO = Monthly,
    YR = Yearly,
    AbbrevEnd
  }

But it doesn't seem to me like that would actually work.  Is there documenation on this?
0
Comment
Question by:mrichmon
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 2

Expert Comment

by:kmaicorp
ID: 16997656
Yes it does work, we used it in our system as well, we even define an enum like so

public enum MyEnum
{
  val1 = 1,
  val2 = 2,
  val3 = 4,
  val4 = val1 | val2
}

As far I documentation, I don't remember where I've seen it before. Just try it in your code and see if it works.
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16997943
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16998276
What doesn't work about it?

Bob
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17002638
kmaicorp,

You are only using a base int type that is not what I am asking about here.  Notice that none of your values overlap.  The | works because you are going in powers of 2.  That is n

anyoneis,

That is the basic documentation for enums and does not address this isse.

TheLearnedOne,

Okay I played with it some more and actually it does work.  I think the AbbrevStart and AbbrevEnd were causing issues, but those can just be excluded.  However is there any documentation that talks about aliasing to earlier enum values?  Also is there a way to just have one set show, but both accpeted for each value?  I guess we are just looking for documenation on how aliasing works so we can see what is and is not possible so we can evaluate if it meets our needs.
0
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.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17002697
kmai

You are not assigning two items to the same value

public enum MyEnum
{
  val1 = 1,
  val2 = 2,
  val3 = 4,
  val4 = val1 | val2
}

assigns val4 a value of 3 (2 | 1) 0010 | 0010 = 0011 = 3

You can do things like this ..

        enum foo {
            Option1 = 1,
            Option2 = 1,
            Option3
        }

or

        enum foo {
            Option1 = 1,
            Option2 = Option1,
            Option3
        }

But you will sometimes get odd behavior when dealing with things like reflection (just happen to have this code from a flags question but it illustrates the point nicely)

            foo f = foo.Option1 | foo.Option2 | foo.Option3;
            foreach(int value in Enum.GetValues(typeof(foo))) {
                if (((int) f & value) > 0) {
                    Console.WriteLine(Enum.GetName(typeof(foo), value) + " set");
                } else {
                    Console.WriteLine(Enum.GetName(typeof(foo), value) + " not set");
                }
            }

Notice that getname returns Option2 for both.

Cheers,

Greg
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17021645
Anyone?

>>is there a way to just have one set show, but both accpeted for each value?  I guess we are just looking for documenation on how aliasing works so we can see what is and is not possible so we can evaluate if it meets our needs.
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 17025228
Good luck! I haven't been able to find any documentation. It worked this way in C++ so I was not surprised to see it working in C#. The language definition is mute on the point.

David
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 200 total points
ID: 17025477
From the 1.0 C# Specification:

"Multiple enum members may share the same associated value. The example
enum Color
{
      Red,
      Green,
      Blue,
      Max = Blue
}
shows an enum in which two enum members—Blue and Max—have the same associated value."

You will find this resource at: http://msdn.microsoft.com/vcsharp/programming/language/default.aspx

David
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 50 total points
ID: 17025509
Aliasing like this will work .. you just have to be very careful when dealing with things like reflection as it will associated only a single item to the value (In particular the GetName function).
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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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