views:

46

answers:

2

I have an interface

Interface:

interface IThing
{
  Enum MyEnum {get;set;}
  string DoAction(MyEnum enumOptionChosen, string valueToPassIn);
}

Concrete implementation:

public class Thing : IThing
{
  public enum MyEnum
  {
    FirstOption,
    SecondOption,
    ThirdOption
  }

  string doAction(MyEnum enumOptionChosen, string valueToPassIn)
  {
    switch(enumOptionChosen)
    {
      case MyEnum.FirstOption:
        x();
        break;
      case MyEnum.SecondOption:
        y();
        break;
      case MyEnum.ThirdOption:
        z();
        break;
    }
  }
}

When I try compiling this I get 'IThing.MyEnum' is a 'property' but is used like a 'type.' I'm missing something about being able to require use of the Enum in the DoAction() signature.

Thanks for any help.

+1  A: 

Firstly, declare your enum outside of the interface/concrete.

Then in your interface:

MyEnum SomeNum {get; set;}

Then in your class:

public MyEnum SomeNum 
{
   get
   {
     ...
   }
   set
   {
      ...
   }
}

Your main problem was you were trying to declare a return type of "Enum" from your interface, when you should be declaring a return type of "MyEnum".

Remember, enum is a type. You can't force a class to implement a "type", only properties/methods.

That being said, i'm scratching my head to try and figure out what you are trying to do.

RPM1984
+2  A: 

In your interface MyEnum is the name of the variable not the type, so it won't be recognized by the compiler. You should be able to use Generics to get this done.

public interface IThing<T>
{
   T MyEnum { get; set; }
   string doAction(T enumOptionChosen, string valueToPassIn);
}

Then you could implement it like this:

public interface IThing<T>
{
    T MyEnum { get; set; }
    string doAction(T enumOptionChosen, string valueToPassIn);
}

public class Something : IThing<Something.SpecialEnum>
{
    public enum SpecialEnum
    {
        Item1,
        Item2,
        Item3
    }

    public SpecialEnum MyEnum { get; set; }

    public string doAction(SpecialEnum enumOptionChosen, string valueToPassIn)
    {
        return "something";
    }
}
bendewey