The New Constructor and Inheritance
VB.NET requires a call to a special subroutine with the special name New before the program can use an instance of any dynamic object. "Static" - that is, "Shared" - objects are different. The differences between Shared and non-shared or "dynamic" objects is covered in the article "Static" (that is, "Shared") versus Dynamic Types in VB.NET.
Because the New subroutine is "special", it doesn't follow all of the rules that other methods have to follow.
When a class is inherited, there are some significant differences. And there are requirements even if you don't inherit the class. If you need more information about VB.NET inheritance, this site has an introduction here: Classes - Base Classes and Derived Classes. A slightly more advanced article shows how to create a custom control using inheritance: An Introduction to Programming a VB.NET Control With Inheritance.
Note: If you get into Intermediate Language - "IL" - code or perhaps read documentation that uses a lot of buzzwords and acronyms, the constructor is sometimes called the CTOR for short. This is the IL code that refers to the constructor.
One way to illustrate this is to code a New subroutine and violate one of the rules. Visual Studio tells you about it.
--------
Click Here to display the illustration
--------
You don't have to explicitly code a New subroutine for every class, and you don't have to code a call to MyBase.New. Visual Studio does that for you. You can't see these statements in your code, but it's there anyway.
Understanding this can be important if you do code New methods for your own classes. They're called in a very specific way. Here's an example. This code instantiates a class that I've named derivedClass just to make things clear.
Here's the code for derivedClass.
Suppose baseClass was outside the control of your project. Maybe it does something unique and is coded by a subcontractor. You know that you have to inherit it and that's all. The thing to be aware of is that whatever is coded in the New subroutine will get executed. If the subcontractor wants to add something "extra" to your application, this is one way that it can be done.
The result of running this code is shown in this illustration.
--------
Click Here to display the illustration
--------
The relationships between classes can become confusing. Visual Studio offers a tool that can clear up a lot of this confusion with a clear visual representation: Classes - The Class Diagram Tool in Visual Studio.
This syntax is enforced because New can't be shadowed or overridden in the derived class. You have to accept the base class definition of New.
Another thing to notice is that the constructors get executed from the base down. If you think about it, it's clear that this happens because the MyBase.New statement in each inherited class is executed first. This "execute first" cascades up to the topmost class and then the New subroutines start executing one at a time until the last child in the chain. (It works this way whether they are in your code or added "behind the scenes" by Visual Studio.)
Because the New subroutine is "special", it doesn't follow all of the rules that other methods have to follow.
When a class is inherited, there are some significant differences. And there are requirements even if you don't inherit the class. If you need more information about VB.NET inheritance, this site has an introduction here: Classes - Base Classes and Derived Classes. A slightly more advanced article shows how to create a custom control using inheritance: An Introduction to Programming a VB.NET Control With Inheritance.
Note: If you get into Intermediate Language - "IL" - code or perhaps read documentation that uses a lot of buzzwords and acronyms, the constructor is sometimes called the CTOR for short. This is the IL code that refers to the constructor.
One way to illustrate this is to code a New subroutine and violate one of the rules. Visual Studio tells you about it.
--------
Click Here to display the illustration
--------
You don't have to explicitly code a New subroutine for every class, and you don't have to code a call to MyBase.New. Visual Studio does that for you. You can't see these statements in your code, but it's there anyway.
Understanding this can be important if you do code New methods for your own classes. They're called in a very specific way. Here's an example. This code instantiates a class that I've named derivedClass just to make things clear.
Dim theClass As New derivedClass
Here's the code for derivedClass.
Class derivedClassInherits baseClassPublic Sub New()Console.WriteLine("New() - In derivedClass")End SubEnd Class
Suppose baseClass was outside the control of your project. Maybe it does something unique and is coded by a subcontractor. You know that you have to inherit it and that's all. The thing to be aware of is that whatever is coded in the New subroutine will get executed. If the subcontractor wants to add something "extra" to your application, this is one way that it can be done.
Public Class baseClassPublic Sub New()'Dim Whatever As String = "Whatever"MyBase.New()Console.WriteLine("New() - In baseClass")Console.WriteLine("This code in baseClass could do anything.")Console.WriteLine("Be sure you're aware that it runs every time.")End SubEnd Class
The result of running this code is shown in this illustration.
--------
Click Here to display the illustration
--------
The relationships between classes can become confusing. Visual Studio offers a tool that can clear up a lot of this confusion with a clear visual representation: Classes - The Class Diagram Tool in Visual Studio.
This syntax is enforced because New can't be shadowed or overridden in the derived class. You have to accept the base class definition of New.
Another thing to notice is that the constructors get executed from the base down. If you think about it, it's clear that this happens because the MyBase.New statement in each inherited class is executed first. This "execute first" cascades up to the topmost class and then the New subroutines start executing one at a time until the last child in the chain. (It works this way whether they are in your code or added "behind the scenes" by Visual Studio.)
Source...