Re: enum convert to char... how?
Wojtek wrote:
buu wrote :
I created my enum type
enum UserStatus{
X, L, D, B, V, N
}
wich I would like to send as an string or char...
how?
there's no .toString() function of (char) casting....
switch-case is last solution...
In addition to the other advice, you can also build your own display:
enum UserStatus
{
X('X'), L('L'), D('D'), B('B'), V('V'), N('N');
private char displayCode;
private UserStatus(char display)
{
displayCode = display;
}
public char getDisplayCode()
{
return displayCode;
}
}
That seems a bit over the top for the case when the string you want to
display is easily derived from the enum value
public class TestUserStatus {
enum UserStatus {
X, L, D, B, V, N ;
}
public static void main(String[] args) {
UserStatus status = UserStatus.X;
System.out.println(status);
System.out.println(status.name());
}
}
If the display code is not exactly the same as the name, you could add
to the enum a method to make use of name() - for example:
public String getDisplayCode() {
return name().toLowerCase();
}
Or you could just override the toString() method as others suggested.