views:

1853

answers:

2

In the code below I recieve the compile error "Error Too many arguments to 'Public Sub New()'" on the "Dim TestChild As ChildClass = New ChildClass("c")". I do not recieve 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. Any assistance would be apprecited.

+2  A: 

Parameterized Constructors cannot be inherited in the same way as instance methods. You need to implement the constructor in the child class and then call the parent's constructor using MyBase.

Public Class ChildClass
    Inherits BaseClass

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

Public Class TestClass
  Public TestChild AS New ChildClass("c")
End Class

This limitation is not VB specific. From what I can gather, it's definately not possible in C#, Java or C++ either.

Here is one related post with the same question about C++:
c-superclass-constructor-calling-rules

Jose Basilio
Thank you for the suggestion. I updated the code as you suggested in visual studio and I still recieve the same compile error. I also updated the post so others will see your suggestion incorporated. Any other ideas?
Bentley Davis
Sorry, I misread your post. The goal is to inherit the new method so I do not have to repeat the code in all my child classes. How can I access the new method in the base class without coding it in the child class as you suggested.
Bentley Davis
Jose, I edited for clarity. I don't need to implement the method 1 in the child class. Is there a requirement to implement constructors in the child class. They can't be inherited directly?
Bentley Davis
Jose, Thank for al you help in clarifying my question. Thank you for refrence to other OO languages. It helps in understanding the concept.
Bentley Davis
I don't have a 15 rep yet. I'll stop back buy when I reach that.
Bentley Davis
+5  A: 

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.

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.

JaredPar
JaredPar, Thanks for the clear answer and pointing to the documentation. I like to have the source so I can dive into the details when possible.
Bentley Davis