2

Is it possible to inherit from two classes in VB.net?

We are developing Custom User Controls that inherits from say System.Web.UI.WebControls.Label. We are planning on implementing a bunch of these controls but they will share mostly the same additional properties. We are hopeful about centralising these properties.

I have looked into interfaces but it seems they only 'contract' properties you need to implement.

Thoughts?

timmah.faase
  • 2,219
  • 1
  • 15
  • 23
  • Consider just using [composition](http://en.wikipedia.org/wiki/Object_composition) instead http://stackoverflow.com/a/178368/284240 – Tim Schmelter Jan 16 '12 at 22:24

2 Answers2

2

No. It is not possible for VB.net types to inherit more than one class. It is possible to implement an infinite number of interfaces but inheritance is limited to one type.

Note: This is not an arbitrary VB.Net restriction. It's actually a restriction that is baked into the CLR. Languages like C++/CLI get around this with some very interesting type flattening routines but at a CLR level they use single inheritance.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thanks JaredPar - any suggestion to the best way we implement this situation? Forget centralising and just implement properties in each? – timmah.faase Jan 16 '12 at 22:22
  • @timmah.faase I would put the common functionality into a single class and then have the users simply have a field of that type and use same named properties to forward into the field type. And likely create interfaces for the shared functionality – JaredPar Jan 16 '12 at 22:24
0

No, you cannot inherit from two classes.

However, we faced exactly the same issue that you do and solved it in the following manner:

1) We created an interface that was implemented by each of the controls so that they could be easily discoverable.

2) We created a settings class that contained settings and logic that were common to all classes.

3) We added a property to the interface created in 1 to expose the settings class so that we could perform standard actions on all properties.

competent_tech
  • 44,465
  • 11
  • 90
  • 113