Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > enum: display elements of an enum specified at runtime

Reply
Thread Tools

enum: display elements of an enum specified at runtime

 
 
Jerminia
Guest
Posts: n/a
 
      10-07-2005
Hello,

I have the following problem:

I need to perform validation on an input parameter that is a String.
This String has to be one of the values of an enumeration.
The problem is that the enumeration type is not known until runtime - thus
the caller tells me that the concerned enum by specifying its full
qualified name.

e.g.
boolean validateStringOnEnum( String toTest, String
enumFullQualName);

Does anybody know what the code would look like?

Cheers!
 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      10-07-2005
Jerminia <(E-Mail Removed)> writes:

> boolean validateStringOnEnum( String toTest, String
> enumFullQualName);


Use Class.forName() to get the enum class, then call the static method
"valueOf" via reflection. toTest would then be passed as the second
argument.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-07-2005
On Fri, 07 Oct 2005 17:07:23 +0200, Jerminia <(E-Mail Removed)>
wrote or quoted :

>I need to perform validation on an input parameter that is a String.
>This String has to be one of the values of an enumeration.
>The problem is that the enumeration type is not known until runtime - thus
>the caller tells me that the concerned enum by specifying its full
>qualified name.
>
>e.g.
>boolean validateStringOnEnum( String toTest, String
>enumFullQualName);
>
>Does anybody know what the code would look like?


An enum has to be known at compile time. What perhaps you want is a
HashMap that converts the legal external strings into some short form
to put in your database.

Try JComboBox for interacting with the user.

See http://mindprod.com/jgloss/jcombobox.html
http://mindprod.com/jgloss/hashmap.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-07-2005
On Fri, 07 Oct 2005 17:07:23 +0200, Jerminia <(E-Mail Removed)>
wrote or quoted :

>the enumeration type is not known until runtime


There are two ways of interpreting that.

1. the enums are all standard enums and the code for them is known an
compile time, but which particular enum applies to a piece of data is
not known until run time.

2. the enums and their constants are not known at compile time. I
have to somehow construct enums on the fly at run time and use them.

Thomas assumed the first interpretation and I the second.

There is another low-tech way of handling the first interpretation.

if ( enumtype.equals("fruit") )
return Fruit.valueOf ( candidate );
else if ( enumtype.equals("vegetable") )
return Vegetable.valueOf ( candidate );

If the number of enums becomes unwieldy, create a HashMap of Delegate
objects indexed by enum name, that each implement
an interface with a valueOf method.

see http://mindprod.com/jgloss/delegate.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
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
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
Problems Compiling enum.h from ENUM++ Randy C++ 1 01-07-2006 10:52 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