Re: Typesafe Enum Pattern
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 3 Oct 2006 07:09:30 +0100
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
.
- References:
- Typesafe Enum Pattern
- From: Steven Nagy
- Re: Typesafe Enum Pattern
- From: Kevin Spencer
- Re: Typesafe Enum Pattern
- From: Steven Nagy
- Re: Typesafe Enum Pattern
- From: Dave Sexton
- Re: Typesafe Enum Pattern
- From: Jon Skeet [C# MVP]
- Re: Typesafe Enum Pattern
- From: Dave Sexton
- Re: Typesafe Enum Pattern
- From: Jon Skeet [C# MVP]
- Re: Typesafe Enum Pattern
- From: Steven Nagy
- Typesafe Enum Pattern
- Prev by Date: Re: Export to excel.
- Next by Date: Re: Typesafe Enum Pattern
- Previous by thread: Re: Typesafe Enum Pattern
- Next by thread: Re: Typesafe Enum Pattern
- Index(es):
Relevant Pages
|