enums & switch statements

How can I make the following situation work?[code]int i = 0;swtich(i) {case SomeEnum.ONE.ordinal():break;}Currently, this does not compile, but I think you get the idea.
[225 byte] By [stoid2004-sun@yahoo.coma] at [2007-11-27 10:07:41]
# 1

let's try that again:

How can I make the following situation work? int i = 0;

swtich(i) {

case SomeEnum.ONE.ordinal():

break;

}

Currently, this does not compile, but I think you get the idea.

stoid2004-sun@yahoo.coma at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 2
> int i = 0;> > swtich(i) {> case SomeEnum.ONE.ordinal():> break;> }> [/code]> Currently, this does not compile, but I think you get> the idea.Your not setting the SomeEnum.ONE.ordinal(): = to
mark07a at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 3
You have to switch on a constant value, not a variable or the return of a method
georgemca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 4

> let's try that again:

>

>

>

> How can I make the following situation work? > int i = 0;

>

> swtich(i) {

> case SomeEnum.ONE.ordinal():

> break;

> }

>

> Currently, this does not compile, but I think you get

> the idea

If you are going to use enums in then i has to be of enum type- it has to be of the same type. So with enums something like this:

enum Order {ONE, TWO, THREE}

Order i = Order.ONE;

switch(i) {

case ONE: //

case TWO:

case THREE:

}

snic.snaca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 5
If I *had* to write such a switch statement, I would switch on the enum value, not the ordinal property
BigDaddyLoveHandlesa at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 6
> You have to switch on a constant value, not a> variable or the return of a methodExactly. So, in other words, there is no way to switch on an integer given only an enum, as I have illustrated?
stoid2004-sun@yahoo.coma at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 7

> Exactly. So, in other words, there is no way to

> switch on an integer given only an enum, as I have

> illustrated?

If you know the ordinal values, you could always write:

switch(i) {

case 0:

...

case 1:

...

case 2:

...

}

BigDaddyLoveHandlesa at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 8

> > You have to switch on a constant value, not a

> > variable or the return of a method

>

> Exactly. So, in other words, there is no way to

> switch on an integer given only an enum, as I have

> illustrated?

No, In other words, you can't use the return value of a method in a "case" clause - it won't compile, as you're finding out

But you can switch on enums themselves. Mixing enums and numerical value is a poor idea, use the type safety or don't - but don't mix both

georgemca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 9

> > You have to switch on a constant value, not a

> > variable or the return of a method

>

> Exactly. So, in other words, there is no way to

> switch on an integer given only an enum, as I have

> illustrated?

You could go something like this

switch(SomeEnum.ONE.ordinal()) {

case 1:

case 2:

}

assuming ordinal() returns an int

snic.snaca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > You have to switch on a constant value, not a

> > > variable or the return of a method

> >

> > Exactly. So, in other words, there is no way to

> > switch on an integer given only an enum, as I have

> > illustrated?

>

> You could go something like this

> > switch(SomeEnum.ONE.ordinal()) {

>case 1:

> case 2:

>

> }

>

> assuming ordinal() returns an int

Which wouldn't be any use at all in this instance. It's an entirely different value he wants to switch on. Simply guessing at combinations of method calls and members that might compile isn't really very useful

georgemca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 11

> int i = 0;

>

> swtich(i) {

>case SomeEnum.ONE.ordinal():

You'd have to do it like this:

SomeEnum someEnumValue = SomeEnumValueOf(i); // see note

switch (someEnumValue) {

case ONE:

NOTE: I don't recall offhand if enums provide a built-in way to get the enum value that corresponds to an int ordinal, or if you'd have ot write it yourself. Left as an exercise for the reader.

jverda at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 12

You could just do:

switch(SomeEnum.values()[i]) {

case ONE:

... etc.

But this is almost certainly poor design. If your code depends on the ordinal values of enums it's not using them properly. Use EnumSet for looping etc. with enums.

malcolmmca at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 13

> You could just do:

>

> > switch(SomeEnum.values()[i]) {

>case ONE:

> etc.

>

I guess that's what I was looking for, IF the order of values() is guaranteed.

> But this is almost certainly poor design.

Ditto!

jverda at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 14
why dont you try a for() loop with the index being the place to access the enum? then you can return aresult, compute or whatver it is you're trying to do
jeanberna at 2007-7-13 0:43:59 > top of Java-index,Java Essentials,Java Programming...
# 15
> IF the order of values() is guaranteed.It is, according to the order in which the enums are declared. But I add my vote for "bad design".~
yawmarka at 2007-7-21 23:16:13 > top of Java-index,Java Essentials,Java Programming...
# 16
> > IF the order of values() is guaranteed.> > It is, according to the order in which the enums are> declared.Thanks for being my research flunky. :-)
jverda at 2007-7-21 23:16:13 > top of Java-index,Java Essentials,Java Programming...
# 17
> Thanks for being my research flunky. :-)I know my place.~
yawmarka at 2007-7-21 23:16:13 > top of Java-index,Java Essentials,Java Programming...