473,834 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading a constructor/default constructor

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

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

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

Private Sub btnTest_Click(. ..) Handles btnClsTest.Clic k
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 6506
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.WriteLi ne("inside default constructor")
End Sub

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

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

Private Sub btnTest_Click(. ..) Handles btnClsTest.Clic k
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**@discussio ns.microsoft.co m> schrieb
Public Class clsTest
Public Sub New()
Console.WriteLi ne("inside default constructor")
End Sub

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

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

Private Sub btnTest_Click(. ..) Handles btnClsTest.Clic k
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.WriteLi ne("inside default constructor")
End Sub

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

Public Sub New(ByVal s As String, ByVal t As String)
Console.WriteLi ne(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**@discussio ns.microsoft.co m> 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 : MyInheritedClas s
{
public clsTest() : base()
{
// Do something.
}

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

public clsTest(string MyParam, string MyOtherParam) :
base(MytOtherPa ram)
{
// 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
4773
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
3006
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 constructor that all parameters have default values
19
6571
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 not declare one? 2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments? Thanks, Andrew
12
5826
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
16051
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
22
3634
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 v);
10
2436
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 virtual function; 2. virtual inheritance; 3.base class with explicit default constructor;
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10503
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.