Solved

Alias for enum values

Posted on 2006-06-27
9
3,287 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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

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

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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