Can we do this quick?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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)
 
If you have a variable of type OrderStatus, use the variable's ToString
method to get a string equal to "new", "paid", etc.
 
Hello,

int x =2;

OrderStatus status = (OrderStatus) x;

string text = status.ToString();

Best regards,
Henning Krause
 
AMercer said:
If you have a variable of type OrderStatus, use the variable's ToString
method to get a string equal to "new", "paid", etc.
....

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

Regards,
Goran
 
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.
 
Back
Top