A base class for two classes which need to inherit different classes

Okay, here’s a good one for you,

      So I’ve developed a custom control based which inherits from System.Windows.Forms.Combobox, which allows users to select a color from the drop down. This control is based off the one on the copeproject website linked below:
http://www.codeproject.com/Articles/34332/Color-Picker-Combo-Box
      The control worked beautify… until I realized I needed to put it inside a ToolStrip (face palm). So I made a copy of the control and made it inherit from ToolStripComboBox. All the code basically stayed the same, the only changes I had to make were that some of the events and properties of the ToolStripComboBox that I needed were only accessible through the ToolStripComboBox.ComboBox property. So the only thing I had to change was that one or two of the subs handed Combobox.EventName instead of Me.EventName.
      This control also worked perfectly, but then I realized I needed to change something, so I went into the code for the ToolStripComboBox color picker and made the change. Off course then I realized I had to make the same change in the original ComboBox color picker. After another few changes that I realized I needed to make, I started to get pretty annoyed at the fact that I needed to write the same pieces of code twice over.
      This is normally when you would use a base class; one class that both controls inherit from. The only problem is that I need access in this class to the base members of the ComboBox class. In order to get those base members, I would have to allow my base class to inherit System.Windows.Forms.Combobox. But then I couldn’t inherit this base class in my ToolStrip color picker class, because it needs to inherit ToolStripComboBox.
      So I’m not exactly sure what to do here. I need the same members in both classes with the same code in each, but they need to inherit from different from different classes.

      Any ideas?
BROOKLYN1950Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lluddenCommented:
Move the code to another class, and have both classes use the methods in that class to do the actual work.

There are probably other ways to do what you want, but since .net doesn't support multiple inheritance, I can't think of anything cleaner.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
When you want to do stuff like that, you need to plan it ahead in your class hierarchy. And Microsoft evidently did not design the framework to enable you to do what you want.

The ComboBox inherits from Control and the ToolStripComboBox inherits from ToolStripItem. They definitively made a difference between the set of controls that run inside a Form and the ones that run inside of a ToolStrip. And it makes sense, because both containers are very different beasts.

And even if there was a way to "arrange it" in a way that would let the compiler accept it, it would not work at runtime. This is because although many of the properties and methods of these controls have the same name, the code for these is not defined at the same place.

If, in your own controls, you change the BackColor for instance, it ends up executing code in the ToolStripControlHost class for the ToolStripComboBox, while the BackColor for the ComboBox is defined in the code of the ComboBox itself. With the framework classes hierarchy that puts both controls in different branches, you cannot play with polymorphism to let the system determine which of these 2 pieces of code to call in a given instance. You need a common line of inheritance in order to enable that.

So even if your code is the same in both of the controls you created, it does not run the same way underneath. So, depending on what you do in your code, even Illudden solution might not work.

And even if, by chance, a common routine works OK today, I would not vouch for it in the future. If Microsoft ever change something in one of the controls that breaks your code in a later version of the framework, correcting it for that control might break the code for the other one. So having 2 copies of the same code now is probably the best way to go anyway, because of the architecture of the framework.
0
BROOKLYN1950Author Commented:
Though this is a pretty obvious solution, and while I do agree there should be a better was of doing this. I have to give this answer credit since it was the only actual solution and was the solution I actually used in the end.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.