473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Enum 'aliasing' and ToString()

Let's say I have an enum like

public enum Aliased {A = 0, B, C, Lowest = A, Highest = C};

and a variable

Aliased Variable = Aliased.C;

When I do Variable.ToString() in simple test code, I get "C". Yet, in
a real app, in similar code, I get the equivalent of "Highest" - ie, I
get the aliased name, not the 'primary' name.

My less important question is: Why the difference between the two? Why
isn't it ALWAYS the first defined or ALWAYS the last defined or ALWAYS
the first alphabetically?

My real question: Is there a way to control which name ToString will
use? I'd expect something like an attribute that I can apply to an
enum member

public enum Aliased
{
A = 0, B, C,
[NoName] Lowest = A,
[NoName] Highest = C
};

.... but there doesn't seem to be anything of the sort?

--

www.midnightbeach.com
Nov 16 '05 #1
3 3498
Jon Shemitz <jo*@midnightbeach.com> wrote:
Let's say I have an enum like

public enum Aliased {A = 0, B, C, Lowest = A, Highest = C};

and a variable

Aliased Variable = Aliased.C;

When I do Variable.ToString() in simple test code, I get "C". Yet, in
a real app, in similar code, I get the equivalent of "Highest" - ie, I
get the aliased name, not the 'primary' name.

My less important question is: Why the difference between the two? Why
isn't it ALWAYS the first defined or ALWAYS the last defined or ALWAYS
the first alphabetically?
I suspect it may well have something to do with hashcodes, but either
way, it's not defined.
My real question: Is there a way to control which name ToString will
use? I'd expect something like an attribute that I can apply to an
enum member


I don't believe there's any way of doing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
[ToString is inconsistent with enum items]
Is there a way to control which name ToString will
use? I'd expect something like an attribute that I can
apply to an enum member


I don't believe there's any way of doing it.


It's not especially elegant, but as a workaround you could apply a
DescriptionAttribute to each enum element and use that description
rather than calling ToString.

P.
Nov 16 '05 #3
"Jon Skeet [C# MVP]" wrote:
My less important question is: Why the difference between the two? Why
isn't it ALWAYS the first defined or ALWAYS the last defined or ALWAYS
the first alphabetically?


I suspect it may well have something to do with hashcodes, but either
way, it's not defined.


Thanks, Jon. You definitely deserve that MVP.
My real question: Is there a way to control which name ToString will
use? I'd expect something like an attribute that I can apply to an
enum member


I don't believe there's any way of doing it.


Thanks. That's what I thought; I switched the code to put the aliases
into a second enum.

--

www.midnightbeach.com
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but...
4
by: avivgur | last post by:
Hi, Suppose I have an enum declared as: enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue; And I want the command "Console.WriteLine(c);" to print out "Dark Blue" instead of...
8
by: cadilhac | last post by:
Hi, I have the following code: public enum MyColors { Red, Green, Blue } MyColors c = (MyColors)Enum.Parse(typeof(MyColors), "abcd"); Why do I get c = "abcd" after this code instead of...
3
by: giant food | last post by:
This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows: Public Enum statusValues Ready Running Finished...
6
by: giannik | last post by:
I have an Enum Structure Public Enum MyEnum EnumVal1=0 EnumVal2=1 EnumVal2=2 end enum I save in an access database this enum value as an integer (0=EnumVal1,
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
6
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.