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. ![]()
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.
-
-
Consider using an Interface?
Perhaps the template method pattern? -
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. -
JohnAskew wrote:Consider using an Interface?
Perhaps the template method pattern?
Unfortunately, the ClassA happens to be a native class of .NET framework so I have little control on that, the best I could hope for is to create a type that is compatible of ClassA.Status so I can directly assign the value of it to a variable of ClassB.StatusEx type.
ScanIAm wrote: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.
No, I won't use StatusEx in any of the methods provided by ClassA.
I just want to way to directly assign the value to the new variable type, instead of using "switch" statement to map it one by one.
(In the analogy of that, assume ClassA.Status to be byte and ClassB.StatusEx to be integer. I wouldn't assign an integer to byte as overflow may occur. And it'd be quite insane to have huge "switch table" to map "1" to "1", then "2" to "2", etc. when doing conversion, right?)
-
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:
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.