• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enum

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm using an enumerated type to contains a set of strings like this:




In my client code, when I want to get a String which belongs to this enumeration I need to use:



This seems a bit long-winded to me. What I really want to do is define an enumeration of Strings, so that I can refer to members of this enumeration in my client code using:



Any idea how I can achieve this?

Thanks in advance,
DM

[ March 19, 2007: Message edited by: Dan Murphy ]
[ March 19, 2007: Message edited by: Dan Murphy ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it really necessary to use the extra variable code there?

If you make the constant names ZERO, ONE, etc., then you can just refer to the constants as MyEnum.ZERO or MyEnum.ONE.
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is what you meant, but if you're referring to the fact that the default implementation of Enum.toString() returns the name of the constant itself, then that doesn't really help me.

The example I've shown above is a simplified version of my actual enum type. In the real version it would not be appropriate to name give the constants the same name as the String contents.

If this is not what you meant, then I'd be very appreciative if you could further explain (perhaps with an example), what you are suggesting.

Thanks,
DM
[ March 19, 2007: Message edited by: Dan Murphy ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand exactly what you're trying to do.

However, you can't make the constants in the Enum be Strings since they inherit from java.lang.Enum.
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I'm trying to do is simply define a fixed-collection of Strings using an Enum. I know I could achieve something similar with:

Set<String> myStrings = new HashSet<String>()

or

Map<String> myStrings = new HashMap<String>()

But neither of these give me the type-safety of an Enum. Also, with an Enum clients can use the Enum constants to easily refer to a member of the enumeration, whereas with a Map I need to define a separate set of keys, and with a Set the entire collection must be iterated over in order to find one particular member.

But from what you said it sounds like this isn't possible
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you using the String returned by the getCode() method for? Would it be more appropriate to pass around the enum itself instead of a String it refers to?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a number of options: redefine the enum's toString method; don't use Strings in the first place; don't use enums. There are probably more - which one is the appropriate very much depends on what you want to do with that set of strings. So, what do you want to do with it?
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to redefine toString(), because this method is supposed to be used to print out a human-readable description for debugging, etc. In other words, a program shouldn't be functionally dependent on toString().

I want to use this Enum to define the valid values of a paramters, which are either 'd' or 't'.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give a description of how you want to use 'd' and 't'?

Also have you looked at name() and valueOf()?
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic