Posted By: cheong | May 4th, 2007 @ 3:20 AM
page 1 of 1
Comments: 5 | Views: 2754
Say that I have a "ClassB" inherited from "ClassA", and "ClassA" has a enum member called "Status":

class ClassA
{
    enum Status
    {
       Status1,
       Status2,
       Status3
    }
    // ... some other code
}


Now I want to extend the functionality of "ClassA" by being able to detect "Status4" and "Status5", so I'm going to define a new enum named "StatusEx". Do I have to create a new enum and copy every status in, or I can do something like this:

class ClassB : ClassA
{
    enum StatusEx : Status
    {
        Status4,
        Status5
    }
    // ... some other code
}

Note: I know the above code won't compile, just to demonstrate the idea of what do I want to achieve.

Thanks for any input. Smiley

P.S.: Actually I wish to have a enum type that can perform one-to-one matching, so I can directly assign a value of Status to StatusEx.
JohnAskew
JohnAskew
9 girl in pink sweater
Consider using an Interface?
Perhaps the template method pattern?
ScanIAm
ScanIAm
On a scale of 1 to 10, people are stupid.
If you wanted, you could create a separate, static class to hold the statuses.  Then, when you started up, each class would feed it's own statuses into the static class where they'd be stored in a hashtable.

If you need to do this programmatically, I can't figure out how the base class could understand a status it's never seen.
odujosh
odujosh
Need Microsoft SUX now!

Enums are only good for list of key value pairs that do not change much if ever. .NET allows you to get away with this (below). If you have too many red flags going up for future matienance issues. You are better off using a Dictionarty Collection or implement some kind of string indexer. Because your design doesnt align very well with the design of enum types.

class Class1

{

enum myEnum { one, two, three };

}

class Class2 : Class1

{

enum myEnum {one, two, three, four, five };

void mymethod()

{

int x = myEnum.five;

int x = myEnum.one;

}

}

I just posted a proposed solution.  Let me know what you think:

 

http://www.codeproject.com/KB/cs/EnumBuilder.aspx

page 1 of 1
Comments: 5 | Views: 2754
Microsoft Communities