This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Nesting enums?

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

I was wondering whether it is possible to nest enums?

My problem is that I want an enum called country and then I want a nested enum within that called state/province, is this possible?

so you could do a call maybe something like this:

State state = Country.US.State.NY;

Thanks in advance,

John
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you definitely can nest enums. But, I don't think you can do what you want. You can mess around with your enum definitions to pass in an enum class (that has the states you're interested in). But at that point, it's a runtime issue and I don't think there's any way you can reference them statically in your code. (You'd have to resort to methods to get at the states you're interested in.)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Bartlett:
Hi,

I was wondering whether it is possible to nest enums?

My problem is that I want an enum called country and then I want a nested enum within that called state/province, is this possible?

so you could do a call maybe something like this:

State state = Country.US.State.NY;

Thanks in advance,

John



Not in the way you want. Realize that your Country enum is a full-fledged class, 1.5 just provides some syntactic sugar to do what we've been doing for a decade. If you were to declare a State enum nested within Country then that would be a nested class like any other. Country.US.State.NY is probably syntactically possible, but it would mean that Country.CANADA.State.NY was just as valid. US and CANADA are both a Country, and State would just be an (implicitly) static nested class of Country.

Perhaps you should describe the "big picture" and we might have some better design suggestions for your problem.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am currently working on a tax related problem. The problem i am trying to overcome is the abilbity to have countries where there are provinces with different tax rates and countries that donot have seperate provinces.

I currently have a Tax Rate class that contains a reference to a Region Class that has a Country object. (or atleast some variant on that).

Any ideas on a better solution would be very appreciated.

Thank you,

John
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can have a State enum where the states know their countries, should that help. Or a Country enum where each country knows what states it has, should that help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic