tobbjo

Hi all.

I want to create my own class MyForm that inherits from Form. But I also want the form to inherit from my own class MyComponent that inherits from Component.

How can I accomplish this

Thanks in advance.

/Tobias



Re: Visual C# Language Multiple inheritance

Sergey Galich

Short answer - you can't. C# deliberately do not support multiple inheritance, because it's hard for majority of people to work with virtual multiple inheritance.

I suggest you to subclass form from what you need and aggregate your component inside.





Re: Visual C# Language Multiple inheritance

tobbjo

That's not what I was hoping for.

Thanks anyway.





Re: Visual C# Language Multiple inheritance

Sergey Galich

Why you need to inherit Form from your Component What is the reason





Re: Visual C# Language Multiple inheritance

tobbjo

I want for example MyGroupbox and MyForm to have a common method for example AddComponent(MyComponent component) that adds the compoent and then maybe resize that component to be 1/10th of the MyForm or MyGroupbox holding it.





Re: Visual C# Language Multiple inheritance

Sergey Galich

I feel you might be interested to learn about IServiceProvider interface, this may help you.

If you want new control to be resized to 1/10th of the container - why not let this control do resize itself For example, your control might have property to specify amount of control's size to be used.

Also I think there might be even simpler solution - use splitter with fixed panel and put your control in this fixed panel.





Re: Visual C# Language Multiple inheritance

tobbjo

That is not all I want to do, I really need to keep track of all the components I add. So I need the MyComponent class. Will look in to IServiceProvider.

Thanks.





Re: Visual C# Language Multiple inheritance

ScubaSteve20001

C# does not support multiple inheritance, but it does support multiple implementations of interfaces, just to throw that out there.




Re: Visual C# Language Multiple inheritance

tobbjo

Damn, cant find a nice solution. C++ is much easier. There I could have MyButton that uses CreateWindow and then inherits from MyComponent and all is fine. Now I'm stuck.




Re: Visual C# Language Multiple inheritance

ScubaSteve20001

You can do the same thing in C#, VB.NET, or Java as long as you use an interface to define the 'common' methods in your classes. A class can then implement as many interfaces as you want.




Re: Visual C# Language Multiple inheritance

tobbjo

Wouldnt I have to write those methods in every class then That's what I want to avoid. I want a method in MyComponent that is reused in all MyForm, MyButton and so on that I create.




Re: Visual C# Language Multiple inheritance

ScubaSteve20001

Yes you would have to provide an implementation in every class since interfaces only specify an actual interface to your class. Something else you could do is create a class that inherits Form, and then in that class you can create whatever methods that you want to share between your classes. And then your MyForm, and MyButton class can inherit from that other class.




Re: Visual C# Language Multiple inheritance

tobbjo

But then MyButton can't inherit from Button.