Solved

Alias for enum values

Posted on 2006-06-27
9
3,017 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

15 Experts available now in Live!

Get 1:1 Help Now