471,347 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

Extend a Predefined Enum

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?

Feb 23 '06 #1
4 2309
>Any thoughts out there?

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 23 '06 #2
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?

Feb 23 '06 #3
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().ToStri ng());
Console.WriteLine(MyEnum.INVALID.ToString());
Console.WriteLine(MyEnum.INVALID.ToOrdinal().ToStr ing());
Console.WriteLine(MyEnum.CLOSED.ToString());
Console.WriteLine(MyEnum.CLOSED.ToOrdinal().ToStri ng());
Console.ReadLine();
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Feb 24 '06 #4
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!

Feb 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Kerberos | last post: by
21 posts views Thread by Andreas Huber | last post: by
31 posts views Thread by Michael C | last post: by
2 posts views Thread by centrino | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
3 posts views Thread by ssg31415926 | last post: by
2 posts views Thread by Dennis | last post: by
3 posts views Thread by eXt | last post: by
2 posts views Thread by =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.