473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 10282
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******@online.microsoft.com> wrote in message
news:f$****************@cpmsftngxa06.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
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...
5
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
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...
6
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...
2
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...
34
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...
3
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
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,...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.