views:

46

answers:

1

I think I know the answer to this but hoping someone has a neat solution. We are currently using two kinds of drop down controls (telerik and .net). I'm hoping to combine these into one control but struggling with a user friendly design.

Ideally the control would be created in the design file with a bool property of say "SimpleBox", to determine which kind of control to inherit. The instantiation would then be generated in the code behind design file and the constructor would then dynamically load the base (which isn't possible). The easy solution would be for me to create a IDropDown interface then have a factory create the correct one. The only real problem with this is the fact the instantiation has to be manually written every time. Which is a hassle, and does not speed up our process at all.

Although it isn't directly possible i'm looking for a solution along the lines of a factory which is ran inside the object constructor for setting the base, based on a bool property.

Cheers

+2  A: 

You might want to look into composition/delegation instead of inheritance here.

In essence, rather than extending either class directly, create a wrapper class that extends Control (or something similarly low-level) and implements IDropDown, add an IDropDown field for the underlying control implementation you want to use, and forward every method call of interest to the selected implementation. This rapidly becomes tedious if there are a lot of methods, though.

Jeffrey Hantin
I thought about this, but to further complicate the problem we currently have controls inheriting from our current base DropDownBox (which will be replaced with the hybrid). The inheriting controls are using protected event handlers, but since you can't have protected members in an interface i was getting all sorts of access issues, perhaps i was doing it wrong.
g.foley
@`g.foley` - As you say, the interface can't have protected methods, but you have to have a base class that implements the interface, and the base class can have protected methods. The `IDropDown` interface can have event which allows your hybrid class to expose the events as needed.
Enigmativity