Deepak
Deepak

Reputation:

Multiple inheritance problem in C#

I am in a situation where i need to use multiple inheritance in C# with WPF.

I am making a control lets say Control-1 that is derived from combobox control. I added some dependency properties as well as methods to the my control Control-1 class. Most of the properties and methods(infact the same implementation of properties and methods) in my control-1 can also be used in another control called Control-2 but that control should not be derived from combobox control (as is the case with Control-1).

I want to seperate the common dependency properties and methods in another class but seperating it in another class require me to derive my control class (control-1) from combobox control and the common class containing properties and methods.

Is there a design that can solve my problem.

Note: The question is about C# using the WPF framework's dependency properties, which require static members and not just on C# in general.

Related

How to reuse code when multiple inheritance is not an option?

Multiple Inheritance in C#

How To Implement Shared Behavior Between Classes (Without Multiple Inheritance Of Course) in C#

What are some good alternatives to multiple-inheritance in .NET?

Upvotes: 5

Views: 2538

Answers (5)

user273201
user273201

Reputation: 1

The problem with extension methods and interfaces is that dependency properties require the declaration of static members and public properties, for example:

    public PermissionEnum Permission
    {
        get { return (PermissionEnum)GetValue(PermissionProperty); }
        set { SetValue(PermissionProperty, value); }
    }

    public static readonly DependencyProperty PermissionProperty =
        DependencyProperty.Register("Permission", typeof(PermissionEnum), typeof(SecurityMenuItem), new FrameworkPropertyMetadata(PermissionEnum.DeliveryView));

Upvotes: 0

Joshua
Joshua

Reputation: 43327

I've used stubs that are called from the derived class and take the class of base type as an argument. This leaves me with several one line functions. Too bad.

Upvotes: 0

Noon Silk
Noon Silk

Reputation: 55172

I can't speak directly to the Dependency Property situation, so I'll talk about the general problem, if that's helpful.

You can't do multiple inheritance of implementation in C#. However, you can attach an interface.

So you can define the interface:

interface IWhatever
{
    ...
}

And then, you can implement the functions of that interface in a class like so:

class M : IWhatever
{
}

And, now, you take the classes that you would like to have this additional functionality on:

class B : MustExtend, IWhatever
{
    private M myMImpl = new M();

   // implement functions, call to 'myMImpl' for implementation.
}

This is called 'composition'. It can be useful in some circumstances, and is generally underused, I'd think :)

Upvotes: 0

recursive
recursive

Reputation: 86144

One solution that may work for you is to create an interface instead, and put your implementation in extension methods.

Upvotes: 5

Related Questions