Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control class multiple inheritance

Status
Not open for further replies.

JontyMC

Programmer
Nov 26, 2001
1,276
GB
I have a form with various controls on; group boxes, panels and text boxes. I have overridden these controls as new classes so they can all have another class, called bind, attached to them. I have an instance of type control, called context, that stores the last control the user clicked on.

The problem is, I want to access the bind class of the context instance. I can do this by checking the type and casting it as that, but that seems a bit rubbish. What I'd really like to do is override the control class, attach the bind class to that, then create other classes which inherit from that and from groupbox, panel or textbox. You cannot, however, have multiple inheritance in .net. I can't figure out a way round this.

Any ideas?

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Inherit from multiple classes in C# cannot be done.
I think this is not a problem. From design perspective, a class does not represent always a fully object. C# supports what is called "Multiple Implementation", which is to says a class can implement more than one interface.
You have to “switch” to think from multiple classes to inherit from only one class but multiple interfaces.
obislavu
 
Thanks for the reply obislavu.

The trouble is, I want to have a general new control class that inherits from the system.windows.forms.control class containing a bind class. But also I want specific controls to inherit from that class and also panel, groupbox and textbox. Can I get round this with interfaces?

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
No. An interface specifies a contract (basically which methods you agree to support) but does not provide an implementation, so you can't inherit them in the same way as you would with a concrete method on a class.

Will the Decorator pattern help you at all here? Basically, write a wrapper class for System.Windows.Forms.Control that holds an instance of the control, plus a Bind class?
 
Thanks steve. I've looked at decorator pattern, but it doesn't really help. I've solved the problem by wrapping all the controls in a panel class which implements the bind. Not ideal, but it'll do.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top