Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructors calling other constructors in vb.net a la c#

Tags:

vb.net

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?

like image 964
Antony Koch Avatar asked Oct 23 '09 09:10

Antony Koch


People also ask

Can a constructor call another constructor?

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.

Can a constructor call another constructor C#?

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.

How do you call a constructor in Visual Basic?

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.

How do you call another constructor?

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”.


1 Answers

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.

like image 161
Joey Avatar answered Oct 14 '22 19:10

Joey