Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to use enum that are not int type in the switch( ) statement in java?

Reply
Thread Tools

How to use enum that are not int type in the switch( ) statement in java?

 
 
Bruce Sam
Guest
Posts: n/a
 
      01-10-2005
/* ******************************* */
class PDomainEnum {
public static final int PDPoint = 0;
public static final int PDLine = 1;
}

class PDomain {
PDomainEnum type;

public void within(type) {
switch(type) {
case PDPoint:
break;
case PDLine:
break;
default:
break;
}
}
}
/* ************************** */
The above is a not a completed program but just to show
something.I want to create an enum PDomainEnum,it has two variable
PDPoint and PDLine.In PDomain,I define an object of PDomainEnum
type.The most important thing that I want to do is use the switch( ) to
select statement.I know switch not allow the non-int type in it.But I
can't find a method to implement the selection statement.Could you give
me some advice?Addtional,I'm using J2SE 1.4.2,and has some reason I
can't not use J2SE1.5.

 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      01-10-2005
The switch statement is just a convenient way of using nested if-else
statements

 
Reply With Quote
 
 
 
 
Andrew McDonagh
Guest
Posts: n/a
 
      01-10-2005
Bruce Sam wrote:
> /* ******************************* */
> class PDomainEnum {
> public static final int PDPoint = 0;
> public static final int PDLine = 1;


> }
>
> class PDomain {
> PDomainEnum type;
>
> public void within(type) {
> switch(type) {
> case PDPoint:
> break;
> case PDLine:
> break;
> default:
> break;
> }
> }
> }
> /* ************************** */
> The above is a not a completed program but just to show
> something.I want to create an enum PDomainEnum,it has two variable
> PDPoint and PDLine.In PDomain,I define an object of PDomainEnum
> type.


This is the problem, you should be defining an int, see below.

The most important thing that I want to do is use the switch( ) to
> select statement.I know switch not allow the non-int type in it.But I
> can't find a method to implement the selection statement.Could you give
> me some advice?Addtional,I'm using J2SE 1.4.2,and has some reason I
> can't not use J2SE1.5.
>


Your problem is that the 'within' method takes an instance of
PDomainEnum Class. Yet the PDomainEnum class is not your enum, but just
the class where the enums PDPoint & PDLine are defined.

so, change the signature of the 'within()' method to accept an 'int' and
call it like this.

PDomain p = new PDomain()

p.within(PDomainEnum.PDLine);



Having said all of this, your 'enums' are just static int's. Which is
fine, but very crude. Have a google for the Type-Safe enum idiom.

Also, google for 'refactoring replace conditional'.

One of the first links should be...

http://www.refactoring.com/catalog/r...ymorphism.html
 
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
Re: an enum question - how to convert enum type to a string? Wojtek Java 1 02-27-2010 03:44 AM
Re: an enum question - how to convert enum type to a string? Eric Sosman Java 3 02-26-2010 03:36 PM
enum type int or unsigned int? kar1107@gmail.com C Programming 10 05-02-2006 06:13 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments