?
Solved

Alias for enum values

Posted on 2006-06-27
9
Medium Priority
?
3,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 800 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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