Patrick
Patrick

Reputation: 2583

How to define enums in a class abiding by an interface

The interface is:

public interface CommonPluginInterface
{
    string GetPluginName();
    string GetPluginType();
    bool ElaborateReport();
}

now I want all derived classes to identify themselves through a string and an enum. For the string it's easy for it's hard coded:

public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
{
    public string GetPluginName()
    {
        return "Foo";
    }
}

but additionally I want it to identify through an enum also. So I thought about putting in the interface but interface can't contain members.

So I thought about making

public class CommonPluginClass
{
    private enum ePluginType { UNKNOWN, EXCEL, EXCEL_SM, RTF}
    private ePluginType pluginType;
}

and making the derived class derive ALSO from that but this is not possible for it says:

Class 'PluginReport_Excel' cannot have multiple base classes: 'MarshalByRefObject' and 'CommonPluginClass'

and I need MarshalByRefObject. Thanks for any help.

Upvotes: 0

Views: 158

Answers (3)

Andriy Tolstoy
Andriy Tolstoy

Reputation: 6090

Define an enum separately and define it as a return type for the GetPluginType method.

public enum ePluginType 
{ 
    UNKNOWN, 
    EXCEL, 
    EXCEL_SM, 
    RTF
} 

public interface CommonPluginInterface
{
    string GetPluginName();
    ePluginType GetPluginType();
    bool ElaborateReport();
}

public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
{
    public ePluginType GetPluginType()
    {
        return ePluginType.EXCEL;
    }

    //implement other interface members
}

Upvotes: 1

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

You can use a property on the interface with the enum´s type:

public interface CommonPluginInterface
{
    string GetPluginName();
    bool ElaborateReport();
    ePluginType PluginType { get; }
}

Now all implementing classes also have to set the property accordingly. However you´ll ned the enumeration to be public.

public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
{
    public string GetPluginName()
    {
        return "Foo";
    }
    public PluginType { get { return ePluginType.Excel; } }
}

Alternativly when you want to use the GetPluginType-method you may simply convert the enums value to string:

public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
{
    public string GetPluginName()
    {
        return "Foo";
    }
    public string GetPluginType()
    {
        return this.PluginType.ToString();
    }
    public PluginType { get { return ePluginType.Excel; } }
}

Upvotes: 1

g t
g t

Reputation: 7463

How about creating a MarshalByRefCommonPluginClass?

public class MarshalByRefCommonPluginClass : MarshalByRefObject
{
    private enum ePluginType { UNKNOWN, EXCEL, EXCEL_SM, RTF}
    private ePluginType pluginType;
}

As an aside, naming conventions would normally mean interfaces have an 'I' prefix (ICommonPlugin) and types would use capital letters (EPluginType).

Upvotes: 1

Related Questions