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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)