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