Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you inherit a sub new (Constructor) with parameters in VB?

In the code below I receive the compile error

Error Too many arguments to 'Public Sub New()' 

on the Dim TestChild As ChildClass = New ChildClass("c"). I do not receive it on TestChild.Method1() even though they are both on the base class I am inheriting from.

Public Class BaseClass     Public ReadOnly Text As String     Public Sub New(ByVal SetText As String)         Text = SetText     End Sub     Public Sub New()         Text = ""     End Sub End Class  Public Class ChildClass     Inherits BaseClass End Class  Public Class TestClass     Sub Test()         Dim TestChild As ChildClass = New ChildClass("c")         TestChild.Method1()     End Sub End Class 

I could change the child class to:

Public Class ChildClass     Inherits BaseClass       Public Sub New (ByVal SetText As String)       MyBase.New(SetText)     End Class End Class 

As suggested below but I do not have to do that for Method 1 or other inherited methods and I am looking for the cleanest code possible. This may be a limitation in the system with inheriting parameterized New statements but I can not find it documented anywhere. If it is required then I would like to see the documentation.

like image 299
Bentley Davis Avatar asked Apr 25 '09 22:04

Bentley Davis


People also ask

Which constructor Cannot inherit?

A parent class constructor is not inherited in child class and this is why super() is added automatically in child class constructor if there is no explicit call to super or this.

How can you pass parameters to the constructors of base classes in multiple inheritance?

To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments along to one or more base class constructors. Here, base1 through baseN are the names of the base classes inherited by the derived class.

Which of the following Cannot be inherited from the base class?

Answer: Constructor cannot be inherited but a derived class can call the constructor of the base class.

Is it possible to inherit the constructors of its base class?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.


1 Answers

The behavior that you are seeing is "By Design". Child classes do not inherti constructors from their base types. A child class is responsible for defining it's own constructors. Additionally it must ensure that each constructor it defines either implicitly or explicitly calls into a base class constructor or chains to another constructor in the same type.

You will need to define the same constructor on all of the child classes and explicitly chain back into the base constructor via MyBase.New. Example

Class ChildClass   Inherits BaseClass   Public Sub New(text As String)     MyBase.New(text)   End Sub End Class 

The documentation you are looking for is section 9.3.1 of the VB Language specification.

  • http://msdn.microsoft.com/en-us/library/aa711964(VS.71).aspx

I think the most relevant section is the following (roughly start of the second page)

If a type contains no instance constructor declarations, a default constructor is automatically provided. The default constructor simply invokes the parameterless constructor of the direct base type.

This does not explicitly state that a child class will not inherit constructors but it's a side effect of the statement.

like image 106
JaredPar Avatar answered Sep 21 '22 10:09

JaredPar