473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Overloading a constructor/default constructor

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
4 6480
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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.