11 Replies - 31837 Views - Last Post: 14 February 2010 - 06:10 PM Rate Topic: -----

#1 Guest_Johan Groth*


Reputation:

Convert an enum to integer

Posted 12 February 2010 - 10:02 AM

Hi all,
suppose you got an enum defined like this:
enum ServerErrorCodes {
  OK(100), NotFound(404);

  private final int id;
  ServerErrorCodes(int id) {
     this.id = id;
  }
}


Then suppose you get back an error code from a server as an integer, how do I convert that integer to enum ServerErrorCodes. I would like to write something like:
int result = getServerService("document");// dummy code just for this example
ServerErrorCodes serverResult = result; // this does not work. What magic is required, though?
switch (serverResult) {
  case OK:
     return true;
  case NotFound:
     return false;
}



Does anyone here know how to do it?

Regards,
Johan

Is This A Good Question/Topic? 0

Replies To: Convert an enum to integer

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Convert an enum to integer

Posted 12 February 2010 - 10:21 AM

You should be able to just cast out the value
int i = (int)ServerErrorCodes.NotFound;

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Convert an enum to integer

Posted 12 February 2010 - 10:59 AM

Have a look at Java Programming - int to Enum
Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,756
  • Joined: 20-September 08

Re: Convert an enum to integer

Posted 12 February 2010 - 11:14 AM

You'd only need to do

return serverResult.getId();



, having given it that method
Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Convert an enum to integer

Posted 12 February 2010 - 11:38 AM

thanks for correcting me, too much C#
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Johan Groth*


Reputation:

Re: Convert an enum to integer

Posted 12 February 2010 - 02:05 PM

God, the subject is wrong! :stupid:
I meant I want to convert an integer to an Enum. If you look at the code (which thankfully describes what I want) that is what I'm trying to do.

So the question again. Does anyone here know how to do that?

Regards,
Johan
Was This Post Helpful? 0

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,756
  • Joined: 20-September 08

Re: Convert an enum to integer

Posted 12 February 2010 - 02:14 PM

Quote

I meant I want to convert an integer to an Enum


In the technical sense of 'convert' that's not possible
Was This Post Helpful? 1
  • +
  • -

#8 jgroth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-January 10

Re: Convert an enum to integer

Posted 12 February 2010 - 02:29 PM

Ok, so the only way to do it is to implement a method that does it like:

ServerErrorCodes getEnum(int i) {
  switch (i) {
    case 100:
      return OK;
    case 404:
      return NotFound;
  }
}



Am I correct in this?

Regards,
Johan
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,756
  • Joined: 20-September 08

Re: Convert an enum to integer

Posted 12 February 2010 - 03:17 PM

You could do

    public ServerErrorCodes fromInt(int i) {
	return i == id? this : null;
    }


Was This Post Helpful? 1
  • +
  • -

#10 jgroth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-January 10

Re: Convert an enum to integer

Posted 14 February 2010 - 08:30 AM

So much more elegant than my solution. :)
Thank you very much.

Regards,
jgroth
Was This Post Helpful? 0
  • +
  • -

#11 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Convert an enum to integer

Posted 14 February 2010 - 09:29 AM

View Postjgroth, on 12 February 2010 - 08:29 PM, said:

Ok, so the only way to do it is to implement a method that does it like:

ServerErrorCodes getEnum(int i) {
  switch (i) {
    case 100:
      return OK;
    case 404:
      return NotFound;
  }
}



Am I correct in this?

Regards,
Johan

why not do something like
public final int OK=100, NotFound=404;


then use the values
switch (serverResult) {
  case OK:
     return true;
  case NotFound:
     return false;
 }


Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Convert an enum to integer

Posted 14 February 2010 - 06:10 PM

if you have to convert an enum into an int it is because your design has a flaw
review your architecture
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1