tag:blogger.com,1999:blog-8001170587821659759.post5652604188091080540..comments2023-08-22T02:10:46.226-07:00Comments on My Brain Hurts: Converting Enumerations to User Readable Strings in .NETKoushhttp://www.blogger.com/profile/11049460148325973407noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8001170587821659759.post-34094812197740557262011-06-25T12:20:15.474-07:002011-06-25T12:20:15.474-07:00Instead of creating the custom attribute, I use th...Instead of creating the custom attribute, I use the Description attribute to achieve the same results with the extension method.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-8749741864800750422010-04-09T15:19:46.354-07:002010-04-09T15:19:46.354-07:00doesn't System.ComponentModel.TypeConverter + ...doesn't System.ComponentModel.TypeConverter + String.Format solves this? :)Johan Hernandezhttps://www.blogger.com/profile/08862160929572222413noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-73248258499223137212009-07-22T11:33:33.579-07:002009-07-22T11:33:33.579-07:00The solution you provided is one I described in my...The solution you provided is one I described in my post, which I did not find ideal:<br />"A common implementation I see is some sort of switch statement that resolves enumerations to a string by way of a switch statement or <b>hash table</b> or something."Koushhttps://www.blogger.com/profile/11049460148325973407noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-64490765454966552932009-07-22T11:28:45.563-07:002009-07-22T11:28:45.563-07:00I have another solution without using attributes. ...I have another solution without using attributes. This is the helper class which stores Dictionary(String, YourEnumeration) inside.<br />It requres neither extension methods (.NET 3.0+ only) nor metadata analysis. Look at the ee.codeplex.comEvgeniyhttp://ee.codeplex.com/noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-55924807747482573302009-05-19T06:38:00.000-07:002009-05-19T06:38:00.000-07:00@MonkeyPushButton: well yes and no, in my case I h...@MonkeyPushButton: well yes and no, in my case I had[1] to convert the GetName to lowercase, and that looks ugly!<br /><br />[1] I had to do that because of a compatibility issue with the binary representation of the object (written in C).Igorhttps://www.blogger.com/profile/09511602909924079726noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-75402930585972512422009-05-19T04:15:00.000-07:002009-05-19T04:15:00.000-07:00If you're mapping to a localised resource, you cou...If you're mapping to a localised resource, you could surely just as well use Enum.GetName and name your resources appropriately?MonkeyPushButtonnoreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-63380956896533756082009-05-12T03:14:00.001-07:002009-05-12T03:14:00.001-07:00Console.WriteLine(BadPassword) would not compile, ...Console.WriteLine(BadPassword) would not compile, so I have no idea what you are talking about...Koushhttps://www.blogger.com/profile/11049460148325973407noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-40401207477367118892009-05-12T03:14:00.000-07:002009-05-12T03:14:00.000-07:00You just made a whole lot of no sense.
I know you...You just made a whole lot of no sense.<br /><br />I know you can operator overload in C++, but you can't overload ToString of an enum. Hence the whole point of this.Koushhttps://www.blogger.com/profile/11049460148325973407noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-38660941079455300532009-05-12T02:56:00.000-07:002009-05-12T02:56:00.000-07:00duh
syntactially pleasing?
how about
Console.Wr...duh<br /><br />syntactially pleasing?<br /><br />how about<br /><br />Console.WriteLine(BadPassword);<br /><br />This is the way any C++/Python programmer would accept:<br /><br />std::cout << "Hola, password verification resulted in code " << AuthenticationResult.BadPassword << std::endl;<br /><br />Stuff like that comes for free once you start using operator>>/<<. You'd still have to provide that implementation, much like in .NET<br /><br />I can live with qualifying the enum id.Amberhttps://www.blogger.com/profile/02588145544781882509noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-90710503217898029352009-05-07T11:26:00.000-07:002009-05-07T11:26:00.000-07:00Yeah, that's how I used to do it also! Extension m...Yeah, that's how I used to do it also! Extension methods and the attribute just make it syntactically pleasing. :)Koushhttps://www.blogger.com/profile/11049460148325973407noreply@blogger.comtag:blogger.com,1999:blog-8001170587821659759.post-13580585553874715922009-05-07T11:10:00.000-07:002009-05-07T11:10:00.000-07:00It so sucks that you can't overwrite ToString() (o...It so sucks that you can't overwrite ToString() (or any enum method), I had this "problem" to, and I decided to write a static method in a class to do convert the enum to string instead of adding the Extension Method.Igorhttps://www.blogger.com/profile/09511602909924079726noreply@blogger.com