By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Overloading a constructor/default constructor

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


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

"Rich" wrote:
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

Nov 21 '05 #2

P: n/a
"Rich" <Ri**@discussions.microsoft.com> schrieb
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

Nov 21 '05 #3

P: n/a
"Rich" <Ri**@discussions.microsoft.com> schrieb:
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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

P: n/a
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.