Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to an enum in the actual class rather than the base class in C#

I am having trouble with this, as I have difficulty properly formulating it too. Making it harder to google it. I will try to explain as clearly as possible. I've simplified the code to make it clearer what my question is

I have an abstract class that has methods and properties that are used by all clases that have this as base class:

public abstract class TheBaseClass{

    //some properties here

    public enum MyEnum{} // this one every class has. It is pretty much empty here. Not sure if this is best practice.

    //some methods here
}

Then there are a number of classes based on this:

public SpecializedClass : TheBaseClass{

    //some more properties here

    public new enum MyEnum{} //every single class has a different enum

    //some more methods here
}

Now, somewhere else in the code, I have a method

public void MyMethod(TheBaseClass baseclassobject){

    //do stuff
    var usedforsomething = ((TheBaseClass.MyEnum)i).ToString() //i being an int. This is used to name something in a plot.
    //do more stuff
}

The reason for using TheBaseClass as a parameter for the method, is that before I had very long code that did what mymethod did for each class derived from TheBaseClass. It is not good to have duplicate code, so I made this method instead and wanted to call it with the parameter SpecializedClass (and many other classes like that also). The problem is that when calling TheBaseClass.MyEnum I naturally get the enum for the BaseClass, not the one from SpecializedClass. I have been experimenting with how to get the right enum in the method regardless of what baseclassobject I give it, but can't seem to find the solution.

How can I get the enum of whatever class baseclassobject is? I have tried some different things, but don't seem to work. The problem I think is that the enum is not a property or method that I can call from the object, but rather need to call ClassName.MyEnum, which I don't have the className in the method.

A solution could be to create a method for each class type, with that specific class type as parameter, but that seems like a lot of duplicate code.

For example if I have 50 different derived classes like SpecializedClass

like image 766
Marshall Avatar asked Sep 04 '18 13:09

Marshall


People also ask

Can enum can be used as a base class for another class?

Inheriting an enum Class from Another ClassWe cannot extend enum classes in Java. It is because all enums in Java are inherited from java. lang.

What is the difference between a class enum and a regular enum?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Can enum and class have same name?

The enum value BAD resides in the general identifier scope, while the class type BAR resides in the class identifier scope. That is the reason why you are allowed to have both an enum value and a class with the same name: both names do not collide.

Can we override enum in C#?

You cannot override enums, and C# does not support return type covariance.


1 Answers

I think reflection would be you only option here.

var usedforsomething = 
      baseclassobject
       .GetType()
       .GetNestedType(nameof(TheBaseClass.MyEnum))
       .GetEnumName(i);

But perhaps a better solution would be to add an abstract function GetName in your base class that your child classes must override.

public abstract class TheBaseClass
{

    public enum MyEnum {a,b }

    public abstract string GetName(int value);
}

public class SpecializedClass : TheBaseClass
{

    public new enum MyEnum {c,d }

    public override string GetName(int value)
    {
        return ((MyEnum)value).ToString();
    }
}

You could than do:

var usedforsomething = baseclassobject.GetName(i);

You can than avoid reflection and also the dependence on the child classes declaring the enum with the specific name MyEnum.

like image 142
Magnus Avatar answered Oct 30 '22 20:10

Magnus