简体   繁体   中英

“Multiple inheritance” - methods for a derived base class

Summary

In VB.NET (.NET 4.0), is it possible to derive ListControl and have the same method available for RadioButtonList , CheckBoxList , DropDownList ?

Edit: I have added the C# tag in the hope that C# will have a solution that can also be used with VB.NET. If that is not the case I will remove the tag to stop confusion.

Details

Using this exceptionally helpful answer , I have written a class (derived from DropDownList ) to allow me to persist attributes on individual .Item objects across post-backs.

I would like to extend this functionality to the RadioButtonList and CheckBoxList controls, but I can't figure out if this is possible - and if it is, how to do it.

This would have been fairly straight forward in C++, as it has the ability for multiple inheritance - but I'm stuck here.

The obvious solution is to replicate the DropDownList derived class, but I would prefer it if the code wasn't repeated 3 times.

Can anybody point me in the right direction, as I'm having a real brain-fart here?


This is roughly what I already have for the derived DropDownList control, and it works perfectly...

<ToolboxData("<{0}:DropDownListPersistant runat=""server""></{0}:DropDownListPersistant>"), DefaultProperty("Text")> _
Public Class DropDownListPersistant
  Inherits DropDownList

  Protected Overrides Function SaveViewState() As Object
    ...
  End Function

  Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    ...
  End Sub
End Class

This is what I tried, but it does not work, because obviously DropDownListPersistant is no longer derived from DropDownList ...

Public Class ListControlPersistant
  Inherits ListControl

  Protected Overrides Function SaveViewState() As Object
    ...
  End Function

  Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    ...
  End Sub
End Class

<ToolboxData("<{0}:DropDownListPersistant runat=""server""></{0}:DropDownListPersistant>"), DefaultProperty("Text")> _
Public Class DropDownListPersistant
  Inherits ListControlPersistant
End Class

Try something with the extension method. I think you can have emulate multiple inheritance with extension methods like below.

http://coding.abel.nu/2012/05/emulating-multiple-inheritance-with-extension-methods/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM