473,839 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending the definition of an Enum

Is it possible to extend the members of an Enum defined in an abstract class
in a derived class?

For instance:
abstract class class1
{
protected enum MyEnum {A, B, C}
}

public class class2 : class1
{
//in this class, the MyEnum should have two extra values D and E so it
becomes {A, B, C, D, E}.
}

Is there a way of doing this, or must I redefine the enum in class2 from
scratch, meaning that if the definition changes in class1, I must also
remember to update it in class2.
Or is there another way of going about this?

Any input is greatly appreciated.

-- Hans De Schrijver
Nov 16 '05 #1
3 10308
Is it possible to extend the members of an Enum defined in an abstract class
in a derived class?

For instance:
abstract class class1
{
protected enum MyEnum {A, B, C}
}

public class class2 : class1
{
//in this class, the MyEnum should have two extra values D and E so it
becomes {A, B, C, D, E}.
}

Is there a way of doing this, or must I redefine the enum in class2 from
scratch, meaning that if the definition changes in class1, I must also
remember to update it in class2.
Or is there another way of going about this?


You cannot do inheritance on a Enum as it is a value type.
--
Adrian Mascarenhas, Developer Division

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Nov 16 '05 #2
Is it possible to extend the members of an Enum defined in an abstract class
in a derived class?

For instance:
abstract class class1
{
protected enum MyEnum {A, B, C}
}

public class class2 : class1
{
//in this class, the MyEnum should have two extra values D and E so it
becomes {A, B, C, D, E}.
}

Is there a way of doing this, or must I redefine the enum in class2 from
scratch, meaning that if the definition changes in class1, I must also
remember to update it in class2.
Or is there another way of going about this?


You cannot do inheritance on a Enum as it is a value type.
--
Adrian Mascarenhas, Developer Division

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Nov 16 '05 #3
If you don't mind a little programmatic skullduggery, you can just create an
implicit conversion from a particular class to an enum type of your choice.

Look for the "implicit" conversion operator in the .NET SDK docs.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Adrian Mascarenhas (MS)" <ad******@onlin e.microsoft.com > wrote in message
news:f$******** ********@cpmsft ngxa06.phx.gbl. ..
Is it possible to extend the members of an Enum defined in an abstract classin a derived class?

For instance:
abstract class class1
{
protected enum MyEnum {A, B, C}
}

public class class2 : class1
{
//in this class, the MyEnum should have two extra values D and E so itbecomes {A, B, C, D, E}.
}

Is there a way of doing this, or must I redefine the enum in class2 from
scratch, meaning that if the definition changes in class1, I must also
remember to update it in class2.
Or is there another way of going about this?

You cannot do inheritance on a Enum as it is a value type.
--
Adrian Mascarenhas, Developer Division

This posting is provided "AS IS" with no warranties, and confers no

rights.
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they
originated.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3432
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
5
9673
by: Francois Grieu | last post by:
Can an enum be used as an array size? In other word, is this legal? enum {n=1}; int a; int main(void){return a;} TIA
4
10286
by: avivgur | last post by:
Hi, Suppose I have an enum declared as: enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue; And I want the command "Console.WriteLine(c);" to print out "Dark Blue" instead of "DarkBlue". In effect, I should probably change the ToString() method that the enum uses to implement space insertion between words. The question is, how do I do this?
6
2907
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I understand correctly, what is actually happening on the calling side is an instance of the enum class FavoriteSport is getting created and sent to the PrintFavorite routine with a default value of type int. Is this correct? Say I have a couple hundred uses...
2
1876
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product Public Enum Status psNormal psCharged End Enum
34
11216
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
3
2151
by: Jens Müller | last post by:
I have a file here with several enums: #ifndef PLANARSEP_OPTIMIZE_H #define PLANARSEP_OPTIMIZE_H enum fund_cycle_behavior_t {PASS_MODE_FIRST, PASS_MODE_BEST, PASS_MODE_ALL};
9
2344
by: subramanian | last post by:
Hello. Consider the following code fragment : enum TestEnum { val1 = 10, val2 = 100, val3 = 1000 }; class Test { public : enum TestEnum { val1 = 1, val2 val3 }; Test(int i = 0, int j = 0, TestEnum val = TestEnum(0)); ...
2
3524
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
0
9855
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
10587
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10649
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
10295
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...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7018
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
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.