views:

381

answers:

6

Hi

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?

+4  A: 

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

recursive
The only problem with this approach is that you'll either have duplicate implementations of the same methods in every implementing class, or you'll need to inject the logic using delegates which can be clumsy and difficult to debug.
free-dom
@free-dom the part which says "and put your implementation in extension methods" mitigates most of those problems.
Rex M
A: 

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 :)

Noon Silk
I agree to your views. But the main problem is of dependency properties. Is there a way i can utilize the same dependency property definations.
A: 

Duplicate of

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#

There was also one recently with this amazing ASCII diagram... now where was it...

Ah yes,

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

harpo
This probably shouldn't be posted as an answer.
Rex M
@Rex, my reasoning is that there were too many to list readably in a comment. But I understand. I considered starting a personal downvoting-of-non-answers policy after seeing this question tonight: http://stackoverflow.com/questions/1344657?sort=oldest#sort-top. This is a pet peeve of mine, when people ask "How do I do X" and the first 5 answers say "You shouldn't do X." That's not an answer.
harpo
But I see, you put the links in the question itself. So I will keep that in mind.
harpo
A: 

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.

Joshua
A: 

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));