|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Can we do this quick?I defined an enum like the follows: public enum OrderStatus { new, paid, processing, shipped } In database, we save 0 for new, 1 for paid, etc. However, when we display to users, we need to show new, paid, etc., i.e., the meaningful words, not numbers. Do we have a quick way to do it? Thanks. (I am using switch{} to conver numbers to words, but I believe there should be a more elegant way to do this since I just use the same names which are already defined in enum) If you have a variable of type OrderStatus, use the variable's ToString
method to get a string equal to "new", "paid", etc. Show quote > I defined an enum like the follows: > > public enum OrderStatus > { > new, > paid, > processing, > shipped > } > > In database, we save 0 for new, 1 for paid, etc. > > However, when we display to users, we need to show new, paid, etc., i.e., > the meaningful words, not numbers. > > Do we have a quick way to do it? Thanks.
Show quote
"AMercer" <AMer***@discussions.microsoft.com> wrote in message Though if you need to localize in different languages it becomes bitnews:B3F46947-28E7-4411-A142-2B957B2FDAB5@microsoft.com... > If you have a variable of type OrderStatus, use the variable's ToString > method to get a string equal to "new", "paid", etc. > > > I defined an enum like the follows: > > > > public enum OrderStatus > > { > > new, > > paid, > > processing, > > shipped > > } .... tricky... Regards, Goran Hello,
int x =2; OrderStatus status = (OrderStatus) x; string text = status.ToString(); Best regards, Henning Krause Show quote "Andrew" <And***@discussions.microsoft.com> wrote in message news:379B79B9-A9F2-4905-96AA-A5ECCA3C9C4B@microsoft.com... > Hello, friends, > > I defined an enum like the follows: > > public enum OrderStatus > { > new, > paid, > processing, > shipped > } > > In database, we save 0 for new, 1 for paid, etc. > > However, when we display to users, we need to show new, paid, etc., i.e., > the meaningful words, not numbers. > > Do we have a quick way to do it? Thanks. > > (I am using switch{} to conver numbers to words, but I believe there > should > be a more elegant way to do this since I just use the same names which are > already defined in enum)
Show quote
> I defined an enum like the follows: This is not normally a good idea. What your users see on screen and what you > > public enum OrderStatus > { > new, > paid, > processing, > shipped > } > > In database, we save 0 for new, 1 for paid, etc. > > However, when we display to users, we need to show new, paid, etc., i.e., > the meaningful words, not numbers. > > Do we have a quick way to do it? Thanks. > > (I am using switch{} to conver numbers to words, but I believe there > should > be a more elegant way to do this since I just use the same names which are > already defined in enum) call the underlying values in code are separate and distinct. They shouldn't normally be tightly coupled like this. Someone may want to change the enumerator's name one day and/or add a new one that isn't suitable for display. They may not even realize you're using the name for display purposes. There are other reasons as well. You know your situation best of course but it's normally a bad idea and someone may reprimand you about it sooner or later. |
|||||||||||||||||||||||