VB.NET - Inheritance - How do I use a parent constructor from within a child constructor?

Posted on 2009-02-23
Last Modified: 2012-05-06
How do I use a parent constructor from within a child constructor?

public class A
dim MyInt as integer
  public sub New(byval i as integer)
    MyInt = i
  end sub
end class

public class B
  inherits A
  dim MyFloat as single
  public sub New(byval i as integer)
    ' <---- I'd like to call the parent constructor here
   MyFloat = i
  end sub
end class
Question by:hamlin11
    1 Comment

    Accepted Solution

    Great question!

    Class AA
        Protected Vertices() As Integer
        Public Sub New()
            Console.WriteLine("AA constructor")
        End Sub
    End Class

    Class BB
        Inherits AA
        Shadows Vertices() As Int64
        Public Sub New()
            Console.WriteLine("BB constructor")
        End Sub
    End Class

    The compiler will auto-insert a call to the no-argument constructor of a parent, otherwise you can specify on the first line of the child constructor a call to a specific parent constructor.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now