In c# you can have
public class Foo { public Foo(string name) { //do something } public Foo(string name, int bar) : this(name) { //do something } }
Is there a VB.Net equivalent?
The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.
To call one constructor from another within the same class (for the same object instance), C# uses a colon followed by the this keyword, followed by the parameter list on the callee constructor's declaration. In this case, the constructor that takes all three parameters calls the constructor that takes two parameters.
In visual basic, if we create a constructor with at least one parameter, we will call it a parameterized constructor and every time the instance of the class must be initialized with parameter values. Following is the example of defining the parameterized constructor in a visual basic programming language.
To call one constructor from another constructor is called constructor chaining in java. This process can be implemented in two ways: Using this() keyword to call the current class constructor within the “same class”. Using super() keyword to call the superclass constructor from the “base class”.
It looks similar to Java in this respect:
Public Class Foo Public Sub New(name As String) ' Do something ' End Sub Public Sub New(name As String, bar As Integer) Me.New(name) ' Do something ' End Sub End Class
Note that you have to use MyBase.New(...)
in case you want to call a constructor of a base class. See also VB.NET OOP Part2 – Understanding Constructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With