Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alias for enum values

Posted on 2006-06-27
9
Medium Priority
?
3,501 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

604 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