473,671 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2414
>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 = "PostFetche d";
}

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.WriteLi ne(MyEnum.OPENE D.ToString());
Console.WriteLi ne(MyEnum.OPENE D.ToOrdinal().T oString());
Console.WriteLi ne(MyEnum.INVAL ID.ToString());
Console.WriteLi ne(MyEnum.INVAL ID.ToOrdinal(). ToString());
Console.WriteLi ne(MyEnum.CLOSE D.ToString());
Console.WriteLi ne(MyEnum.CLOSE D.ToOrdinal().T oString());
Console.ReadLin e();
}
}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
4745
by: Kerberos | last post by:
I have a main div with 2 divs inside, one of them floating to the right (they look like 2 columns). The main div has a background image. How can I have the main div to adapt to the maximum height of the divs inside of it so that the background image of the main div repeats vertically according to the height of the 2 divs inside? This way, the content of one of the 2 divs wouldn't overlap the main div, but push its border.
21
4587
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
3590
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
2
2211
by: centrino | last post by:
hi, I want to extend System.Net.Sockets.ProtocolType to IPinIP protocol, how can i do that. Do i have to make new class or enum stuct for this type ? regards
18
11342
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
3
11177
by: ssg31415926 | last post by:
I have an abstract base class with an enum which is passed into a method. I want my derived classes to be able to add new values to the enum without having to redefine it and I want to be able to pass them into the method defined into the base class. Is there a way to do this. E.g. abstract public class BaseWidget {
2
3392
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function and return the name "FirstData"
3
2704
by: eXt | last post by:
For a realtime-application (half an application at least, kind of a framework) I am working with I need a event managing system and naturally it must be fast. I have defined a set of available event types in an enumeration like this: enum Events { EVENT_FOO, EVENT_BAR, EVENT_BAZ, EVENT_CUSTOM1,
2
2692
by: =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
Is there any way to tell PHP predefined variables ($GLOBALS, $argv, $argc, $_GET, $_POST…) from *global* user-defined variables? Neither $GLOBALS nor get_defined_vars() put user data apart. I’m writing a class to generate PHP definition files for syntax highlighting with two features: 1. It takes data from current install (e.g., you get function names from loaded extensions). 2. You can choose whether to include user data or not.
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.