• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

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:
      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?
1 Solution
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.
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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now