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)

RE: Can we do this quick? by AMercer

AMercer
Wed Mar 28 10:00:02 CDT 2007

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


Re: Can we do this quick? by Henning

Henning
Wed Mar 28 10:06:32 CDT 2007

Hello,

int x =2;

OrderStatus status = (OrderStatus) x;

string text = status.ToString();

Best regards,
Henning Krause

"Andrew" <Andrew@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)


Re: Can we do this quick? by Goran

Goran
Wed Mar 28 10:14:23 CDT 2007


"AMercer" <AMercer@discussions.microsoft.com> wrote in message
news: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
> > }
...

Though if you need to localize in different languages it becomes bit
tricky...

Regards,
Goran



Re: Can we do this quick? by Larry

Larry
Wed Mar 28 11:59:04 CDT 2007

> 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)

This is not normally a good idea. What your users see on screen and what you
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.