On 16/2/07, Jonathan Heizer <[EMAIL PROTECTED]> wrote:
Hey all,

I am trying to reuse a bunch of Class members, properties, and functions
that are shared across multiple classes.  Each of these classes already
inherit form a web control so I can not create a base class supporting the
function since .Net does not support multiple inheritance.  Right now each
Inherited class has a complete copy of this code that makes it hard to make
changes to it.

So invision...
MyControl1--\                   /---BaseControl1
MyControl2--->Common Code Class<----BaseControl2
MyControl3--/                   \---BaseControl3

What I really need is multiple inheritance...

Public Class MyLabel
       Inherits System.Web.UI.WebControls.Label)
       Inherits MyCommonStuff

End Class

Since so many of these are shared properties and members using basic
interfaces would not really save that much code space.  What I was trying to
do is mix in generics and do something along these lines...

Public Class MyBaseControl(Of t)
       Inherits t

       Class members...
       Functions...
       Properties...
End Class

Public Class MyLabel
   Inherits MyBaseControl(Of System.Web.UI.WebControls.Label)

End Class

Public Class MyTextbox
   Inherits MyBaseControl(Of System.Web.UI.WebControls.Textbox)

End Class

So what I need is to somehow make my intermediate class inherit the base
control of my choice at runtime based on the type passed through the
generic.  Is anything like this possible?  I have not had any luck so far.

Thanks for any help,

The best I came up with:

Public Interface MyBaseInterface(Of T As System.Web.UI.WebControls.WebControl)

   Sub SomeAdditional(ByVal a As T, ByRef b As T)

End Interface

Public Class MyBaseClass(Of T As {MyBaseInterface(Of T),
System.Web.UI.WebControls.WebControl})

   Shared Sub SomeAdditional(ByVal Base As T, ByVal a As T, ByRef b As T)
       ' Infinite recursion, but good enough as an example
       Base.SomeAdditional(a, b)
   End Sub
End Class

Public Class MyLabel
   Inherits System.Web.UI.WebControls.Label
   Implements MyBaseInterface(Of MyLabel)

   ' Boilerplate
   Public Sub SomeAdditional(ByVal a As MyLabel, ByRef b As MyLabel)
Implements MyBaseInterface(Of MyLabel).SomeAdditional
       MyBaseClass(Of MyLabel).SomeAdditional(Me, a, b)
   End Sub
End Class

Public Class MyTextbox
   Inherits System.Web.UI.WebControls.TextBox
   Implements MyBaseInterface(Of MyTextbox)

   ' Boilerplate
   Public Sub SomeAdditional(ByVal a As MyTextbox, ByRef b As
MyTextbox) Implements MyBaseInterface(Of MyTextbox).SomeAdditional
       MyBaseClass(Of MyTextbox).SomeAdditional(Me, a, b)
   End Sub
End Class

Effectively you're implementing multiple inheritance manually... The
code in the boilerplate routines just calls MyBaseClass where the
detailed code goes. The IDE helps fill out the routine signatures in
the "Boilerplate" code when you complete the Class Implements line.

Not sure if that helps...
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to