0

I have 2 custom server control classes. One inherits from TextBox and the other inherits from Label.

Both classes contain quite a lot of common code (various properties and attribute rendering logic), but the classes do contain some distinct code as well.

Ideally, the common code would be contained in an abstract class, and each custom control would inherit from that abstract class. This is not possible of course, because they already inherit from their respective base classes and c# doesn't support multiple inheritence. How do I resolve the code duplication issue?

Matt
  • 195
  • 1
  • 4
  • 12
  • Implementing an interface sounds like an idea. – Jean Carlos Suárez Marranzini Oct 17 '12 at 14:54
  • @JeanCarlosSuárezMarranzini: Surely he would still have to write the code twice then? Interfaces are more about other objects treating them the same than they are about having the same behaviour (where usually inheritance would work). – Chris Oct 17 '12 at 14:54
  • 1
    [This](http://www.codeproject.com/Articles/10072/Simulated-Multiple-Inheritance-Pattern-for-C) link can help you. – Jean Carlos Suárez Marranzini Oct 17 '12 at 15:02
  • This is asp.net? Please tag the UI. – paparazzo Oct 17 '12 at 15:18
  • @JeanCarlosSuárezMarranzini Yes I have created an interface that both controls implement, but this doesn't solve the issue, it merely helps with the design of code that makes use of these controls. – Matt Oct 19 '12 at 11:42
  • @JeanCarlosSuárezMarranzini Thanks for the link, but I'm not certain this can apply to server controls, baring in mind the properties have to be available in the aspx code. But maybe I have overlooked something. – Matt Oct 19 '12 at 11:46

1 Answers1

0

It depends on the exact functionality. If there is no way for them to inherit from the same place (as seems to be the case) then the best bet is to have a third class that contains the common functionality and include that in some way, either by having an instance of it in your control classes or by just calling static methods on it. Which I'd go for is probably dependant on what your code is and would hopefully be relatively obvious.

Chris
  • 27,210
  • 6
  • 71
  • 92