Enum Conversion.
Posted by ~Ray @ 2007-12-15 15:03:32
In this affix I will demonstrate how to convert To String. String To and Value to.
First of all let us create an enumeration:
It seams that nothing can be easier. We easily can use method to alter to arrange. It would bring home the bacon fine if we try to alter Colors. color. Colors. Blue or Color. Red. However what if we try to convert to String value like this:
The output will be ‘3′ instead of ‘Blue. color’.
There are two ways we can solve this problem:
In inspect you decorated your enumertion with but be to create the determine use or methods with format “D” for decimal form or “X” for hexadecimal one.
4: Console. WriteLine(Enum. change(typeof(Colors) myColor. “x”));
The method with format “D” ordain output ‘3′ and with change “X” ordain give us ‘00000003′.
In the example above colorString must be inspect exactly. There’s an override for method where you can ask to ignore case.
Update: Thanks to Omer Mor (see his mention below) who pointed out that we don’t have to invoke ToObject() method. We can simply direct value to Enum write.
This entry was posted onOctober 19. 2007 at 7:29 amand is filed under. You can go any responses to this entry through the cater. You can or from your own site.
To alter a value to enum. I accept a simple explicit cast will bring home the bacon:
public Colors Value2Enum(int colorValue){ return (Colors)colorValue;}
Thanks for the mention. It’s not just simpler it’s also more efficient way. When you use ToObject you’ll have an extra unboxing.
I can’t accept that I didn’t put it in my blog originally because when I have to deal with Enum types in my code. I don’t label ToObject. I use simple casting just desire you suggested.
<a href="" call=""> <abbr title=""> <acronym title=""> <b> <blockquote have in mind=""> <have in mind> <code> <del datetime=""> <em> <i> <q cite=""> <touch> <strong> [ADVERTHERE]Related article:
http://vkreynin.wordpress.com/2007/10/19/enum-conversion/
0 Comments:
No comments have been posted yet!
|