Overloading a constructor/default constructor

G

Guest

Public Class clsTest
Public Sub New()
Console.WriteLine("inside default constructor")
End Sub

Public Sub clsTest(ByVal s As String)
Console.WriteLine(s)
End Sub

Public Sub clsTest(ByVal s As String, ByVal t As String)
Console.WriteLine(s & " " & t)
End Sub
End Class
-----------------------------------------------------

Private Sub btnTest_Click(...) Handles btnClsTest.Click
Dim testCls As New clsTest
testCls.clsTest("test")
testCls.clsTest("test1", "test2")
testCls = Nothing
End Sub
-------------------------------------------------------

I observed here that I cannot overload the default constructor in VB.Net
which always gets called on invocation of Class clsTest -- Dim testCls As New
clsTest. I am thinking like this:

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")

Question: is/are

Public Sub clsTest(ByVal s As String)
....
Public Sub clsTest(ByVal s As String, ByVal t As String)
....

constructor(s) or just overloaded methods in class clsTest? I am thinking
that if you define a method in a class with the same name as the class - this
constitutes a constructor. Could someone set me straight how to overload a
constructor in VB.Net so I can do

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")

Thanks,
Rich
 
G

Guest

Nevermind. I was able to overload my default constructor. If I had not made
this post, then I would still be having the problem :).
 
A

Armin Zingler

Rich said:
Public Class clsTest
Public Sub New()
Console.WriteLine("inside default constructor")
End Sub

Public Sub clsTest(ByVal s As String)
Console.WriteLine(s)
End Sub

Public Sub clsTest(ByVal s As String, ByVal t As String)
Console.WriteLine(s & " " & t)
End Sub
End Class
-----------------------------------------------------

Private Sub btnTest_Click(...) Handles btnClsTest.Click
Dim testCls As New clsTest
testCls.clsTest("test")
testCls.clsTest("test1", "test2")
testCls = Nothing
End Sub
-------------------------------------------------------

I observed here that I cannot overload the default constructor in
VB.Net which always gets called on invocation of Class clsTest --
Dim testCls As New clsTest. I am thinking like this:

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")

Question: is/are

Public Sub clsTest(ByVal s As String)
...
Public Sub clsTest(ByVal s As String, ByVal t As String)
...

constructor(s) or just overloaded methods in class clsTest? I am
thinking that if you define a method in a class with the same name
as the class - this constitutes a constructor. Could someone set
me straight how to overload a constructor in VB.Net so I can do

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")


The subs "clsTest" are just methods. Nothing else. Constructors are always
named "New". I you need to overload the constructor(s) name them "New":

Public Class clsTest
Public Sub New()
Console.WriteLine("inside default constructor")
End Sub

Public Sub New(ByVal s As String)
Console.WriteLine(s)
End Sub

Public Sub New(ByVal s As String, ByVal t As String)
Console.WriteLine(s & " " & t)
End Sub
End Class


Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")


Armin
 
H

Herfried K. Wagner [MVP]

Rich said:
I observed here that I cannot overload the default constructor in VB.Net
which always gets called on invocation of Class clsTest -- Dim testCls As
New
clsTest. I am thinking like this:

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")

Question: is/are

Public Sub clsTest(ByVal s As String)
...
Public Sub clsTest(ByVal s As String, ByVal t As String)
...

constructor(s) or just overloaded methods in class clsTest?

They are just overloads of the method 'clsTest'. All constructors are named
'New':

\\\
Public Sub New()
...
End Sub

Public Sub New(ByVal a As Integer)
...
End Sub
///
 
M

Mythran

constructor(s) or just overloaded methods in class clsTest? I am thinking
that if you define a method in a class with the same name as the class -
this
constitutes a constructor. Could someone set me straight how to overload
a
constructor in VB.Net so I can do

Dim a As New clsTest
Dim b As New clsTest("test")
Dim c As New clsTest("test1", "test2")

Thanks,
Rich

As the other posts comment on, the constructors are always named "New", in
VB.Net. In C#, though, the constructors are always the name of the class
they are constructing:

public class clsTest : MyInheritedClass
{
public clsTest() : base()
{
// Do something.
}

public clsTest(string MyParam) : base()
{
// Do something here.
}

public clsTest(string MyParam, string MyOtherParam) :
base(MytOtherParam)
{
// Do something here.
}
}


Just showing some extra stuff that is possible when inheriting as well
(calling the base class' constructor's for example).

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top