Extend a Predefined Enum

C

Coder

Hi,

Running into an issue. I am creating a base class. This base class
has a "State" property of type "States" which is a locally defined enum
type. The issue is this...the derived class apparently can't add to
the "States" enum. In other words, if the derived class wanted to
define another state, it can't.

At this point, I am considering making the State proerty an int that
stores enum values. If the derived class wants to, they can define
their own set of states (cut and past the States enum from the base
class and append to it). The State property won't care since all it
does is stores enums.

Any thoughts out there?
 
M

Mattias Sjögren

Any thoughts out there?

Another option is to promote the state type to a class and represent
the different states with objects.


Mattias
 
C

Coder

Thanks Mattias. Great idea.

I would like to avoid creating a class for each state though. Based on
your comment I'm leaning towards:

public class BatchStates
{
public static string PreFetched = "PreFethed";
public static string PostFetched = "PostFetched";
}

However, I don't want my State property to be of type "string". I want
to limit what the values can be for State if possible.

I could do this if I created a class that represents each state but I
don't want the users of my framework have to create a class per custom
state. I would rather they just extend the BatchStates class and
append their custom states.

Any thoughts?
 
J

Jeff Louie

This may or may not work for you:

class MyEnum
{
private String name;
private static int nextOrdinal= 1;
private int ordinal= nextOrdinal++;
private MyEnum(String name)
{
this.name= name;
}
public override String ToString()
{
return name;
}
public int ToOrdinal()
{
return ordinal;
}
public static MyEnum INVALID= new MyEnum("Invalid"); // ordinal 1
public static MyEnum OPENED= new MyEnum("Opened"); // ordinal 2
public static MyEnum CLOSED=new MyEnum("Closed"); // ordinal 3
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Console.WriteLine(MyEnum.OPENED.ToString());
Console.WriteLine(MyEnum.OPENED.ToOrdinal().ToString());
Console.WriteLine(MyEnum.INVALID.ToString());
Console.WriteLine(MyEnum.INVALID.ToOrdinal().ToString());
Console.WriteLine(MyEnum.CLOSED.ToString());
Console.WriteLine(MyEnum.CLOSED.ToOrdinal().ToString());
Console.ReadLine();
}
}

Regards,
Jeff
 
C

Coder

Jeff,

Thanks for the reply and especially for sharing the code. I hope you
didn't go to too much trouble to write that code.

I was thinking on the same lines. Doing it this way the developer can
extend MyEnum and add to the list of "Invalid", "Open", etc. Also, the
State property will be of type MyEnum which will limit the states.

For now, I have gone with just have a class contain static strings and
have the State property of type string. I may end up using your idea
though depending on what the team says. Thanks!
 

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

Top