Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

enum.ToString()

0 views
Skip to first unread message

Ken Allen

unread,
May 12, 2004, 2:00:58 PM5/12/04
to
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 how does one override the
function to have some values return a different string?

For example, suppose I have an enum with value for the types of DVD media
like

public enum DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus,
DVD_RAM
}

but I want the values to print as "DVD-ROM", "DVD-R", "DVD-RW", "DVD+R",
"DVD+RW", and "DVD-RAM" -- the C# syntax does not seem to permit me a
mechanism to override the ToString() method on this specific enumeration.

-Ken


AlexS

unread,
May 12, 2004, 5:13:31 PM5/12/04
to
Hi, Ken

you might consider creating a function, which returns resource string using
as argument enum value and use this function instead of ToString().
enum.ToString() could be used to identify resource strings.

HTH
Alex

"Ken Allen" <kend...@sympatico.ca> wrote in message
news:%23wFiVsE...@TK2MSFTNGP11.phx.gbl...

RBisch

unread,
May 12, 2004, 2:22:03 PM5/12/04
to
Ken,

Try Enum.GetNames(typeof(A_CTS_Type))
====================
RBisch - C# enthusiast
ryanbischoff@PLZyahooNO_SPAM.com


"Ken Allen" <kend...@sympatico.ca> wrote in message
news:%23wFiVsE...@TK2MSFTNGP11.phx.gbl...

Nicholas Paldino [.NET/C# MVP]

unread,
May 12, 2004, 2:23:41 PM5/12/04
to
Alex and Ken,

I think that using a function is a bad idea. Not because it is the
wrong thing to do, but there are better solutions that fit into the model of
what .NET offers. Namely, you should use attributes to attribute the
elements in the enumeration. For something like this, I prefer the
Description attribute in the System.ComponentModel namespace. With it, you
can do this:

public enum MyEnum
{
[Description("My first value.")]
Value,
[Description("My second value.")]
Value2
}

Once you do this, you can use reflection on the enumeration to get the
fields (the values are really just static fields) and then get the
attributes, and subsequently the description.

The advantage to doing this is that the information about the
enumeration stays with the enumeration. Additionally, you can have one
routine which will give you the information, and that's it, you won't have
to change it when you want to change the description.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"AlexS" <salexr...@SPAMsympaticoPLEASE.ca> wrote in message
news:%23y0QtzE...@tk2msftngp13.phx.gbl...

Ken Allen

unread,
May 12, 2004, 2:46:02 PM5/12/04
to
This sounds good, but how do I (simply) "use reflection" to get the
description?

So I declare a variable as "MyEnum enumValue;" and later on I assign a
specific value to this variable. How do I display the descriptive string
rather than the name of the enumerated value?

On another note, am I correct in my assessment that there is no mechanism to
override the ToString() methods on built-ins, and especially value types?

Is there not a format string that will format the description and not the
name or value of the enumeration variable?

-Ken

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:%23KK1B6E...@tk2msftngp13.phx.gbl...

AlexS

unread,
May 12, 2004, 5:49:27 PM5/12/04
to
Nicholas,

attributes are Ok when you don't think about localization. I think Ken, when
doing UI stuff, must consider what to do when another enum, like days of
week has to be displayed in non-English language

So, please correct "bad idea". It's not worse than yours.

HTH
Alex

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:%23KK1B6E...@tk2msftngp13.phx.gbl...

Ken Allen

unread,
May 12, 2004, 3:01:22 PM5/12/04
to
OK, now we are treading into more unfamiliar waters here. I have not worked
with resources in .Net yet, although I have used them in the past with C++
4/5/6.

My understanding is that resources are supposed to be implemented in a
satellite assemebly, but my application does not need that yet, and this
seems more complicated that I need right now.

Have any samples on how to setup and access these resource strings as the
translation of an enumerated value?

-Ken

"AlexS" <salexr...@SPAMsympaticoPLEASE.ca> wrote in message

news:uNXwyHFO...@TK2MSFTNGP09.phx.gbl...

Nicholas Paldino [.NET/C# MVP]

unread,
May 12, 2004, 3:02:34 PM5/12/04
to
Ken,

See inline:

"Ken Allen" <kend...@sympatico.ca> wrote in message

news:OQJ1gFFO...@TK2MSFTNGP10.phx.gbl...


> This sounds good, but how do I (simply) "use reflection" to get the
> description?

Assuming that you are using the Description attribute, you can do this:

public static string GetEnumValueDescription(object value)
{
// Get the type from the object.
Type pobjType = value.GetType();

// Get the member on the type that corresponds to the value passed in.
FieldInfo pobjFieldInfo = pobjType.GetField(Enum.GetName(pobjType,
value));

// Now get the attribute on the field.
DescriptionAttribute pobjAttribute = (DescriptionAttribute)
(pobjFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false)[0]);

// Return the description.
return pobjAttribute.Description;
}

You should probably provide some error checking, but I think you get the
point.

>
> So I declare a variable as "MyEnum enumValue;" and later on I assign a
> specific value to this variable. How do I display the descriptive string
> rather than the name of the enumerated value?
>
> On another note, am I correct in my assessment that there is no mechanism
to
> override the ToString() methods on built-ins, and especially value types?
>
> Is there not a format string that will format the description and not the
> name or value of the enumeration variable?

No, there is not.

Nicholas Paldino [.NET/C# MVP]

unread,
May 12, 2004, 3:16:21 PM5/12/04
to
Alex,

"Bad" is a subjective term.

When it comes to localization, the routine can EASILY be changed so that
it will include a resource identifier which can be used to fetch the
description from a resource, instead of embedding it in the attribute.


--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"AlexS" <salexr...@SPAMsympaticoPLEASE.ca> wrote in message

news:uNXwyHFO...@TK2MSFTNGP09.phx.gbl...

AlexS

unread,
May 12, 2004, 6:49:19 PM5/12/04
to
Nicholas,

I never use bad in sense of good. Maybe too inflexible :-)

But about your remark - combination of Description attribute and resources
is probably most flexible and convenient implementation, which should be
used to document and present in UI not just enums, but also other objects.
I find also another advantage in resources - when you need to change later
some description, you don't need to change code.


And Ken,

you can start from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptutorials/html/resourcemanager.asp -
there are samples how to retrieve strings from resources and links to
further information how to create resources for assemblies.

HTH
Alex

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in

message news:O3CudXFO...@TK2MSFTNGP09.phx.gbl...

Brad Williams

unread,
May 13, 2004, 3:59:14 PM5/13/04
to

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:O3CudXFO...@TK2MSFTNGP09.phx.gbl...

> "Bad" is a subjective term.

It's a contextual term -- and I think you make a good argument that in most
contexts using attributes would be best because the code is more cohesive.
In the less common context where one doesn't want to take the hit for
reflecting, a case statement might make a better choice.

Brad Williams


0 new messages