|
| enum.ToString() |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| 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
|
|
|
| |
|
| |
| |
| 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
"Ken Allen" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
| |
| |
| Nicholas Paldino [.NET/C# MVP] (VIP) |
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] - Click here to reveal e-mail address
"AlexS" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| 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 [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
| |
| |
| Nicholas Paldino [.NET/C# MVP] (VIP) |
Ken,
See inline:
"Ken Allen" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
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.
[Original message clipped]
No, there is not.
-- - Nicholas Paldino [.NET/C# MVP] - Click here to reveal e-mail address
[Original message clipped]
|
|
|
| |
|
|
| |
|
|
| |
| 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 [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| 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" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
| |
|
| |
| Nicholas Paldino [.NET/C# MVP] (VIP) |
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] - Click here to reveal e-mail address
"AlexS" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| | |
| |
| Brad Williams |
"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... > "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
|
|
|
| |
|
| |
|
|
|
|
|
| | |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|