enum: display elements of an enum specified at runtime

Discussion in 'Java' started by Jerminia, Oct 7, 2005.

  1. Jerminia

    Jerminia Guest

    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!
     
    Jerminia, Oct 7, 2005
    #1
    1. Advertising

  2. Jerminia <> 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.
     
    Tor Iver Wilhelmsen, Oct 7, 2005
    #2
    1. Advertising

  3. Jerminia

    Roedy Green Guest

    On Fri, 07 Oct 2005 17:07:23 +0200, Jerminia <>
    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.
     
    Roedy Green, Oct 7, 2005
    #3
  4. Jerminia

    Roedy Green Guest

    On Fri, 07 Oct 2005 17:07:23 +0200, Jerminia <>
    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.
     
    Roedy Green, Oct 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Cheng[MSFT]
    Replies:
    4
    Views:
    4,858
    Steven Cheng[MSFT]
    Jan 12, 2006
  2. Tom
    Replies:
    1
    Views:
    447
    Roedy Green
    Oct 3, 2005
  3. Foxpointe
    Replies:
    1
    Views:
    412
    Dimitre Novatchev
    Jul 27, 2006
  4. Jens Kallup
    Replies:
    1
    Views:
    355
    scripts.contact
    Jun 4, 2007
  5. gniagnia
    Replies:
    5
    Views:
    99
    Dr.Ruud
    Feb 16, 2007
Loading...

Share This Page