Using a variable as an enum value

R

RVic

I have a class which has some

public enum GGFieldName {
RLEN("[0-9]{4}"),
CHG("[\\p{Alnum}]{1}");

private String regexp;

private GGFieldName(String aRegexp) { this.regexp = aRegexp;}

public String regexp() { return this.regexp;}
}

I have another class that has a member variable:

class MyClass {
String s;
}

And s can sometimes equal say the value in the enums of my GGFieldName
class, i.e.
s.equals("RLEN") or s.equals("CHG").

How can I access then the regexp value corresponding to a given value
for s ? I have tried
s.regexp() but fo course that wont even compile. Thanks for your help.
 
J

John B. Matthews

RVic said:
I have a class which has some

public enum GGFieldName {
RLEN("[0-9]{4}"),
CHG("[\\p{Alnum}]{1}");

private String regexp;

private GGFieldName(String aRegexp) { this.regexp = aRegexp;}

public String regexp() { return this.regexp;}
}

I have another class that has a member variable:

class MyClass {
String s;
}

And s can sometimes equal say the value in the enums of my
GGFieldName class, i.e. s.equals("RLEN") or s.equals("CHG").

How can I access then the regexp value corresponding to a given value
for s ? I have tried s.regexp() but fo course that wont even compile.
Thanks for your help.

<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>

"In addition, if E is the name of an enum type, then that type has the
following implicitly declared static methods:
....
public static E[] values();
....
public static E valueOf(String name);"

So,

String s = "RLEN";
String r = GGFieldName.valueOf(s).regexp();
 
R

Roedy Green

I did not quite follow you, but I have code samples on how to create
alias enum constant names. Using regexes would be a fairly simple
variant.

See http://mindprod.com/jgloss/enum.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Let us pray it is not so, or if it is, that it will not become widely known."
~ Wife of the Bishop of Exeter on hearing of Darwin's theory of the common descent of humans and apes.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top