Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > enum and switch

Reply
Thread Tools

enum and switch

 
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
From the compiler complaints I guess this is not possible, yet....

----------------------------
public class MyClass
{
public enum Things
{
ONE,TWO
}

public boolean process(Things thing)
{
boolean success = false;

switch ( thing )
{
case Things.ONE:
success = doStuff(1);
break;

case Things.TWO:
success = doStuff(2);
break;
}

return success;
}
------------------------------

Is there a way to use an enum in a switch statement?

--
Wojtek


 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      03-07-2007
On Wed, 07 Mar 2007 16:32:03 -0000, Wojtek <> wrote:

> From the compiler complaints I guess this is not possible, yet....
>
> ----------------------------
> public class MyClass
> {
> public enum Things
> {
> ONE,TWO
> }
>
> public boolean process(Things thing)
> {
> boolean success = false;
>
> switch ( thing )
> {
> case Things.ONE:
> success = doStuff(1);
> break;
>
> case Things.TWO:
> success = doStuff(2);
> break;
> }
>
> return success;
> }
> ------------------------------
>
> Is there a way to use an enum in a switch statement?


You don't need to qualify the enum constants. In fact, you are only
allowed to use unqualified names. So it should be:

switch (thing)
{
case ONE:
// Do something.
case TWO:
// Do something else.
}

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
 
 
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
Daniel Dyer wrote :
> On Wed, 07 Mar 2007 16:32:03 -0000, Wojtek <> wrote:
>
>> From the compiler complaints I guess this is not possible, yet....
>>
>> ----------------------------
>> public class MyClass
>> {
>> public enum Things
>> {
>> ONE,TWO
>> }
>>
>> public boolean process(Things thing)
>> {
>> boolean success = false;
>>
>> switch ( thing )
>> {
>> case Things.ONE:
>> success = doStuff(1);
>> break;
>>
>> case Things.TWO:
>> success = doStuff(2);
>> break;
>> }
>>
>> return success;
>> }
>> ------------------------------
>>
>> Is there a way to use an enum in a switch statement?

>
> You don't need to qualify the enum constants. In fact, you are only allowed
> to use unqualified names. So it should be:
>
> switch (thing)
> {
> case ONE:
> // Do something.
> case TWO:
> // Do something else.
> }
>
> Dan.


OK, but what if the enum is from another class? I must then qualify it.

BTW I ended up using an if/else if tree, though I would rather use a
switch/case.

--
Wojtek


 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      03-07-2007
On Wed, 07 Mar 2007 17:36:49 -0000, Wojtek <> wrote:

>>> Is there a way to use an enum in a switch statement?

>>
>> You don't need to qualify the enum constants. In fact, you are only
>> allowed to use unqualified names. So it should be:
>>
>> switch (thing)
>> {
>> case ONE:
>> // Do something.
>> case TWO:
>> // Do something else.
>> }
>>
>> Dan.

>
> OK, but what if the enum is from another class? I must then qualify it.
>
> BTW I ended up using an if/else if tree, though I would rather use a
> switch/case.


Did you try it with the switch statement and the unqualified labels? The
compiler knows what class the enum constants are from because it knows the
type of the argument to the switch statement.

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
Daniel Dyer wrote :

>
> Did you try it with the switch statement and the unqualified labels? The
> compiler knows what class the enum constants are from because it knows the
> type of the argument to the switch statement.


It works.

I would not have thought of this.....

Thanks!

--
Wojtek


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 10:06 am, "Daniel Dyer" <"You don't need it"> wrote:
> On Wed, 07 Mar 2007 17:36:49 -0000, Wojtek <nowh...@a.com> wrote:
> >>> Is there a way to use an enum in a switch statement?

>
> >> You don't need to qualify the enum constants. In fact, you are only
> >> allowed to use unqualified names. So it should be:

>
> >> switch (thing)
> >> {
> >> case ONE:
> >> // Do something.
> >> case TWO:
> >> // Do something else.
> >> }

>
> >> Dan.

>
> > OK, but what if the enum is from another class? I must then qualify it.

>
> > BTW I ended up using an if/else if tree, though I would rather use a
> > switch/case.

>
> Did you try it with the switch statement and the unqualified labels? The
> compiler knows what class the enum constants are from because it knows the
> type of the argument to the switch statement.
>
> Dan.
>
> --
> Daniel Dyerhttp://www.uncommons.org


Or, even better... Ever hear of Polymorphism?

// Look ma! No Switches!
public class MyClass {
public enum Things {
ONE {
public boolean doStuff(MyClass myClass) {
return myClass.doStuff(1);
}
},
TWO {
public boolean doStuff(MyClass myClass) {
return myClass.doStuff(2);
}
}
;
public abstract boolean doStuff(MyClass myClass);

}

public boolean process(Things thing) {
return thing.doStuff(this);
}

private boolean doStuff(int i) {
return false;
}
}

 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      03-07-2007
In article <>, Wojtek <>
wrote:

> Daniel Dyer wrote :
>
> >
> > Did you try it with the switch statement and the unqualified labels? The
> > compiler knows what class the enum constants are from because it knows the
> > type of the argument to the switch statement.

>
> It works.
>
> I would not have thought of this.....
>
> Thanks!


A more careful examination of the compiler error would actually tell you
this. I learned it the hard way after adopting a practice in an ongoing
project of *always* qualifying our enum references. The compiler
squawked and I had to read it several times to fully grasp that it was
telling me I could not do so.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
Daniel Dyer wrote :
>
> The
> compiler knows what class the enum constants are from because it knows the
> type of the argument to the switch statement.


Argh, inconsistencies drive me nuts...

So the compiler knows about the enum constants from the switch, but If
I have:

if ( thing == ONE )

That fails because I have not qualified the enum constant.

--
Wojtek


 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
Daniel Pitts wrote :
> On Mar 7, 10:06 am, "Daniel Dyer" <"You don't need it"> wrote:
>> On Wed, 07 Mar 2007 17:36:49 -0000, Wojtek <nowh...@a.com> wrote:
>>>>> Is there a way to use an enum in a switch statement?
>>>> You don't need to qualify the enum constants. In fact, you are only
>>>> allowed to use unqualified names. So it should be:
>>>> switch (thing)
>>>> {
>>>> case ONE:
>>>> // Do something.
>>>> case TWO:
>>>> // Do something else.
>>>> }

>>
>>>> Dan.

>>
>>> OK, but what if the enum is from another class? I must then qualify it.
>>> BTW I ended up using an if/else if tree, though I would rather use a
>>> switch/case.

>>
>> Did you try it with the switch statement and the unqualified labels? The
>> compiler knows what class the enum constants are from because it knows the
>> type of the argument to the switch statement.
>>
>> Dan.
>>
>> --
>> Daniel Dyerhttp://www.uncommons.org

>
> Or, even better... Ever hear of Polymorphism?
>
> // Look ma! No Switches!
> public class MyClass {
> public enum Things {
> ONE {
> public boolean doStuff(MyClass myClass) {
> return myClass.doStuff(1);
> }
> },
> TWO {
> public boolean doStuff(MyClass myClass) {
> return myClass.doStuff(2);
> }
> }
> ;
> public abstract boolean doStuff(MyClass myClass);
>
> }
>
> public boolean process(Things thing) {
> return thing.doStuff(this);
> }
>
> private boolean doStuff(int i) {
> return false;
> }
> }


Well, ok, except that there is more happening in each case than a
method call. And I need to take the time to sit down and actually READ
about Java 1.5+

Sigh, not enough hours in each day, and those damn deadlines.....

--
Wojtek


 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      03-07-2007
Steve W. Jackson wrote :

> A more careful examination of the compiler error would actually tell you
> this. I learned it the hard way after adopting a practice in an ongoing
> project of *always* qualifying our enum references. The compiler
> squawked and I had to read it several times to fully grasp that it was
> telling me I could not do so.


You are right of course. It is just that I am so used to qualifying
things, and along comes this new "feature".

The last thing I would have expected is that the compiler would qualify
it for me AND cause an error if I did.

Fully qualifying the constant should have produced a warning about an
unnecessary cast. IMHO of course

--
Wojtek


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between enum class and enum struct Brian C++ 4 02-27-2010 04:03 PM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57