views:

260

answers:

8

How can I do this:

Class A : DependencyObject {}

Class B : DependencyObject {}

Class C : A , B {}
+3  A: 

You can't, C# does not support multiple inheritance of classes.

If you provide a more concrete example of what you're trying to do, maybe we can give you a better solution (for example, perhaps composition is what you're after, rather than inheritance - of course, I can't tell from this simple example).

Dean Harding
+1  A: 

You cannot have C be both an A and a B unless one of those inherits from the other.

However, if you want C to have behaviours from A and B that are not common to both of them, use an interface.

Graphain
+20  A: 

C# does not have multiple inheritance, so the line

Class C : A , B {}

will never work. You can do similar things with interfaces though, along the lines of

interface InterfaceA { void doA(); } 
class A : InterfaceA { public void doA() {} }

interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}

class C : InterfaceA, InterfaceB
{
  m_A = new A();
  m_B = new B();

  public void doA() { m_A.doA(); }
  public void doB() { m_B.doB(); } 
}
Nicholas M T Elliott
+1 Simple. To the point. Clear.
Joel Etherton
+6  A: 

You can't do multiple inheritance in C# but you can implement multiple interfaces:

  1. Create an interface with the same methods as the public members of A
  2. Create an interface with the same methods as the public members of B
  3. Create member variables for A and B inside of C.
  4. Implement the A interface from both A and C (C's implementation simply calls its member variable of type A)
  5. Implement the B interface from both B and C (C's implementation simply calls its member variable of type B)
Brian R. Bondy
or you could use composition and blaze your own trail. :)
CrazyJugglerDrummer
Composition, FTW.
codekaizen
+1  A: 

Another way, which does not use composition, uses extension methods. Define interfaces with no methods, and implement them with your C class. Then, write a static class with extension methods that provide implementations against your interface.

public static void MyMethod(this InterfaceA a)
{
    // do stuff with a
}

downside of this is that you can only work with those properties of the object that are defined in the interface. This limits you to auto-implemented properties and method calls, basically.

Tesserex
+1  A: 

For better or worse C# does not support multiple inheritance. However, I have had limited success in simulating it by using extension methods. The extension method approach might look like this.

public class A
{
  public void DoSomething() { }
}

public static class B
{
  public static void DoSomethingElse(this A target) { }
}

public class C : A
{
}

The obvious problem here is that C is definitely not a B. So the only thing this is good for is avoiding duplicate code in some situations.

On the other hand C# does support implementing multiple interfaces. It would look like this.

public interface IA
{
  void DoSomething();
}

public interface IB
{
  void DoSomethingElse();
}

public class A : IA
{
  void DoSomething() { }
}

public class B : IB
{
  void DoSomethingElse() { }
}

public class C : IA, IB
{
  private A m_A = new A();
  private B m_B = new B();

  public void DoSomething() { m_A.DoSomething(); }

  public void DoSomethingElse() { m_B.DoSomethingElse(); }
}

The obvious problem here is that while you are inheriting the interface you are not inheriting the implementation. This is good for polymorphism, but bad for avoiding duplicate code.

Sometimes you can use both strategies together to cludge together something resembling multiple inheritance, but it is probably going to be difficult to understand and maintain. If your situation really does call for multiple inheritance then your options are going to limited and certainly not ideal, but they do exist nevertheless.

Brian Gideon
I happen to think C#'s exclusion of MI is for the worse. But that is because I mostly think the complexity argument is overblown. I guess we all have our opinions, but this is one I am most outspoken about...for better or worse of course :)
Brian Gideon
A: 

So I guess it cant be done so ...

class A : DependencyObject
    {
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(A), new UIPropertyMetadata(0));
    }

    class B : DependencyObject
    {
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(B), new UIPropertyMetadata(0));
    }

    class C : DependencyObject
    {
        A a = new A();
        B b = new B();
        public int X
        {
            get { return a.X; }
            set { a.X = value; }
        }
        public int Y
        {
            get { return b.X; }
            set { b.X = value; }
        }
    }
Delta
+1  A: 

Hi,

You can achieve this by Multilevel Inheritance...

 public class DependencyObject
{
    public void DependencyObjectMethod() { }
}

public class A : DependencyObject
{
    public void MethodA() { }
}

public class B : A
{
    public void MethodB() { }
}

public class C : B
{
    public void MethodC()
    {
        //Do Something
    }
}

By this you can get access to all the methods and properties of those classes.

Samdrain