tags:

views:

152

answers:

5

I need a member of my class to be a Control, and for it to implement an interface we define.

If I declare it like this...

public class MyClass
{
    public Control MyMember;
}

... then I don't get the interface methods, but if I declare it like this...

public class MyClass
{
    public IMyInterface MyMember;
}

...then I don't get the Control methods. Is there a way to specify that MyMember must be initialised to a type that inherits from both? I can't find one on MSDN. Something like...

public class MyClass
{
    public Control : IMyInterface MyMember;
}

... or ...

public class MyClass
{
    public Control MyMember : IMyInterface;
}

... except that neither of those work. Can I specify interfaces when I declare a member, and if so, how?

A: 

Use the power of inheritance on the interface

public interface IMyInterface : Control
{
  ..
}

Now you say you want a control with some special methods.


EDIT: TcKs is of course right.. you can't inherit an interface from a concrete class.

One way of solving this problem could be to extend the interface with a property or method that returns a control.

Sample:

public interface IMyInterface 
{
  Control Control { get; }

  [..rest of the definition..]
}

and implement it like that:

class MyControl : Control, IMyInterface
{
  public Control Control { get { return this; } }

  [..rest of the implementation..]
}
VVS
Interface can not inherits from class.
TcKs
Of course you're right.. what was I thinking.
VVS
After edit, I haven't any other remarks :).
TcKs
Still not what I'm after. I don't want to change the interface, I just want to specify a variable that will hold a Control that implements it.
Simon
TcKs
+2  A: 

You can use generics with constraints:

public interface MyClass {
    public T GetMyControl() where T : Control, IMyInterface { /* ........ */ }
}
TcKs
That works for methods, but what about other members?
Simon
Other type members can not be generic.
TcKs
All member types *can* be generic but only methods and types can have type parameters.
Mark Cidade
A: 

You can have your own class derived from control with interface defined like:

class MyControl : Control, IMyInterface
{
}

and then use this class as the member:

public class MyClass
{
    public MyControl MyMember;
}
nullDev
Yes, but that's not what I want. I don't want MyMember to have to subclass MyControl, I just want it to have to subclass control and IMyInterface.
Simon
You can't always get what you want.
Robert S.
So your answer is 'no' :-)
Simon
+1  A: 

Since it's very cumbersome to write a wrapper class around Control and a simple generic class:

public class MyGenericClass<T> where T : Control, IMyInterface
{
    public T t;
}

might not fit your needs. You could simply use different properties to access the field differently:

public class MyClass
{
    private IMyInterface m_field;
    public Control FieldAsControl
    {
     get { return m_field as Control; }
    }
    public IMyInterface Field
    {
     get { return m_field; }
     set
     {
      if (m_field is Control)
      {
       m_field = value;
      }
      else
      {
       throw new ArgumentException();
      }
     }
    }
}
A: 

This sounds fundamentally wrong.

Does IMyInterface declare only the same methods/properties as found in the Control class? If not, what do you hope to accomplish? You cannot implement an interface and ignore the methods declared within it - you must explicitly write out the implementations.

If IMyInterface does, in fact, declare only the same methods/properties as found in the Control class, you will have to create your own MyControl class which inherits from Control and implements IMyInterface. This isn't some stupid quirk of the language. The Control class wasn't defined to be an IMyInterface and C# is a statically typed - not a "duck-typed" - language, so it cannot automatically give Control whatever interface you desire.

Cybis
I'm not trying to ignore one or the other - I want to be able to call methods from both Control and IMyInterface.I *do* have classes which inherit from Control and which implement IMyInterface, but they don't share an ancestor. I want to be able to assign any of them to MyMember.
Simon