enum.ToString()

K

Ken Allen

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
 
R

RBisch

Ken,

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

Nicholas Paldino [.NET/C# MVP]

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

Ken Allen

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 said:
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]
- (e-mail address removed)

AlexS said:
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

that
was
 
K

Ken Allen

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

message news:%[email protected]...
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]
- (e-mail address removed)

AlexS said:
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

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
 
N

Nicholas Paldino [.NET/C# MVP]

Ken,

See inline:

Ken Allen said:
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]
- (e-mail address removed)
-Ken

message news:%[email protected]...
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]
- (e-mail address removed)

AlexS said:
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

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

AlexS said:
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

message news:%[email protected]...
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]
- (e-mail address removed)

AlexS said:
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

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
 
A

AlexS

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
 
A

AlexS

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 said:
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]
- (e-mail address removed)

AlexS said:
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

that
was
 
A

AlexS

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/d...ry/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 said:
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]
- (e-mail address removed)

AlexS said:
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

message news:%[email protected]...
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]
- (e-mail address removed)

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

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
 
B

Brad Williams

Nicholas Paldino said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top