Re: Typesafe Enum Pattern

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Steven Nagy <learndotnet@xxxxxxxxxxx> wrote:
I haven't done much .NET coding in anger since discovering how useful
Java classes are - and not since seeing some reasonably elegant ways of
impersonating the design in C#. I can say that trying to produce
designs in .NET which mirror the equivalent Java enums by using a C#
enum and help classes has led to much nastier code in the C# side, in
my experience.

So whats the actual current solution here?
Jon, you are obviously very passionate about improvements to the enum
in future releases, but my issues exist now. That's why I found my way
to the type-safe enum pattern to begin with. What do you think of this
pattern, or can you suggest a better way? My previous code snippet was
based on some examples I found in java. I want to make sure I do this
properly the first time, because I might actually be using them in my
DAL.

The best implementation I've seen is the one in the comments for my
blog:

public abstract class ArithmeticOperation
{
private ArithmeticOperation()
{
}

public abstract int Eval(int x, int y);

public static readonly ArithmeticOperation Addition = new Add();

private class Add : ArithmeticOperation
{
public override int Eval(int x, int y)
{
return x + y;
}
}

public static readonly ArithmeticOperation Multiplication =
new Multiply();

// ...
}

This uses the fact that nested classes can use private constructors.
The only big downside of this is that you can't use the values in a
switch statement - and that it's a bit ugly, having to declare the
value separately from the class.

Also, imagine the very common scenario where you have a table in DB
with static values. Something like what I have specified above in
chess... ways that a game could be ended (WhiteWins, BlackWins, Draw).
Because these values are not going to change for the life of the
solution, I want to have these represented as an enum. Do you think
using the ts enum pattern is a reasonable approach to representing that
mode? I'm sure state pattern would be applicable somewhere as well, but
for now I'm not managing the game's state, just creating passable
values to represent that state. Is this the best approach?

I would always first consider whether you can do everything you need
with a "simple" enum. If you can't, the pattern above seems pretty
reasonable to me.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Property accessors "add" and "remove" with event properties and EventHandlerList
    ... don't see why event delcarations need to be available at compile-time ... collection with some sort of enum key - but then you'd need to ... the Observer pattern and the Chain of Command pattern. ... extent of that idiom is. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: enum type declaration error
    ... there is C equivalent "enum" type existed in JAVA. ... initial experiment is giving compile error as below. ... public enum returnStatus ...
    (comp.lang.java.programmer)
  • Re: Jython inherit from Java class
    ... Compiling .java to .class... ... 'assert' is a keyword, and may not be used as an identifier ... release 1.5, 'enum' is a keyword, and may not be used as an identifier ...
    (comp.lang.python)
  • Re: Typesafe Enum Pattern
    ... posted is the ultimate pattern. ... be useful in identifying more requirements to be addressed by your proposed "class enum" type, which seems to address Steven's needs ... A pattern or base class to use without any CLR/framework/compiler ... either the Enum or State pattern, but find a place somewhere in between. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A class that uses instances of itself? Is this right?
    ... > I was recently sent some Java code that I find VERY odd. ... > public String getText() { ... 'TypeSafe enum' pattern. ... public void aMethodDoingSomethingOnEnum{ ...
    (comp.lang.java.programmer)