473,320 Members | 1,766 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,320 software developers and data experts.

constructors

thanks for the help...just one more question....

can a class have more then two parameterized constructors?..i would like to
be able to instanciate the class with a different number of argument.....

thanks folks

steve


Nov 20 '05 #1
6 2370
Stephen,
Constructors can be overloaded just like other methods, as long as the
parameter types are different.

Public Class SomeClass

Public Sub New()
End Sub

Public Sub New(ByVal i As Integer)
End Sub

Public Sub New(ByVal s As String)
End Sub

Public Sub New(ByVal ParamArray args() As String)
End Sub

End Class

You can even include optional parameters, however I normally favor
overloading to the Optional keyword.

As I stated in my other post, overloading constructors, base classes & other
OOP concepts are explained in Robin A. Reynolds-Haertle's book "OOP with
Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from MS Press covers this plus the
rest of OOP in VB.NET, in a very straight forward manner.

OOP concepts in VB.NET is also covered in this section of MSDN.
http://msdn.microsoft.com/library/de...ithObjects.asp

Of course you are welcome to ask in the newsgroups also.

Hope this helps
Jay

"Stephen Martinelli" <st*****@johnstontrading.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
thanks for the help...just one more question....

can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of argument.....

thanks folks

steve


Nov 20 '05 #2
Hi Stephen,

You can have as many as you like so long as they are distinct.

These two are the same, despite having different variables
Sub New (Foo As Integer, sSomething As String)
Sub New (Bar As Integer, sDifferent As String)

This is because the <signature> is the same - ie. they both have the same
parameter types. Sub New (Integer, String)

Methods which have the same name but different parameter types
(signatures) are called 'overloads'. Overloading is very handy - as you're
discovering. ;-)

Regards,
Fergus
Nov 20 '05 #3
On 2003-10-23, Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
Stephen,
Constructors can be overloaded just like other methods, as long as the
parameter types are different.

Public Class SomeClass

Public Sub New()
End Sub

Public Sub New(ByVal i As Integer)
End Sub

Public Sub New(ByVal s As String)
End Sub

Public Sub New(ByVal ParamArray args() As String)
End Sub

End Class

You can even include optional parameters, however I normally favor
overloading to the Optional keyword.


Me too. Probably because I code mostly in C#, and C# doesn't support
optional paramters. Also, Optional can cause problems latter on since
the default values actually get compiled into the code and become part
of the interface. If you latter need to change the default it has the
potential to lead to incompatabilities between versions.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #4
I've found though, that constructors are special and don't work the same as
other overloaded members. If you overload a constructor like this:

Public Class A
Public Sub New()
End Sub

Public Sub New(x as string)
End Sub

Public Sub new(y as integer)
End Sub
End Class

And then you make a new class (B) that inherits from "A", the overloaded
constructors won't be available. This is only the case with constructors,
do this overloading with any other class member and the overloaded versions
are available in the derived classes.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
Stephen,
Constructors can be overloaded just like other methods, as long as the
parameter types are different.

Public Class SomeClass

Public Sub New()
End Sub

Public Sub New(ByVal i As Integer)
End Sub

Public Sub New(ByVal s As String)
End Sub

Public Sub New(ByVal ParamArray args() As String)
End Sub

End Class

You can even include optional parameters, however I normally favor
overloading to the Optional keyword.

As I stated in my other post, overloading constructors, base classes & other OOP concepts are explained in Robin A. Reynolds-Haertle's book "OOP with
Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from MS Press covers this plus the rest of OOP in VB.NET, in a very straight forward manner.

OOP concepts in VB.NET is also covered in this section of MSDN.
http://msdn.microsoft.com/library/de...ithObjects.asp
Of course you are welcome to ask in the newsgroups also.

Hope this helps
Jay

"Stephen Martinelli" <st*****@johnstontrading.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
thanks for the help...just one more question....

can a class have more then two parameterized constructors?..i would like

to
be able to instanciate the class with a different number of argument.....
thanks folks

steve



Nov 20 '05 #5
* "Scott M." <s-***@badspamsnet.net> scripsit:
I've found though, that constructors are special and don't work the same as
other overloaded members. If you overload a constructor like this:

Public Class A
Public Sub New()
End Sub

Public Sub New(x as string)
End Sub

Public Sub new(y as integer)
End Sub
End Class

And then you make a new class (B) that inherits from "A", the overloaded
constructors won't be available. This is only the case with constructors,
do this overloading with any other class member and the overloaded versions
are available in the derived classes.


ACK. Constructors are not inherited from the base class.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Scott,
Of course, as Herfried pointed out constructors are NEVER inherited, it does
not make sense to inherit a constructor, as each class requires its own
constructor.

If you do not give a class a constructor one will be provided for you,
provided that your base class has a default constructor. The constructor
provided for you is called the default constructor and has no parameters.

If you provide your class with any constructors, the default constructor
will not be provided for you. In this case if you need a parameterless
constructor you need to provide it yourself.

Again each class requires its own constructor, except in the case of
providing a default (parameterless constructor) the compiler cannot and
should not attempt to decide what a constructor should look like, as the
designer of the class knows best what constructors the class needs!

This is all covered in the link I gave.

Thanks for the catch.
Jay

"Scott M." <s-***@badspamsnet.net> wrote in message
news:uw**************@TK2MSFTNGP09.phx.gbl...
I've found though, that constructors are special and don't work the same as other overloaded members. If you overload a constructor like this:

Public Class A
Public Sub New()
End Sub

Public Sub New(x as string)
End Sub

Public Sub new(y as integer)
End Sub
End Class

And then you make a new class (B) that inherits from "A", the overloaded
constructors won't be available. This is only the case with constructors,
do this overloading with any other class member and the overloaded versions are available in the derived classes.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:eC**************@TK2MSFTNGP12.phx.gbl...
Stephen,
Constructors can be overloaded just like other methods, as long as the
parameter types are different.

Public Class SomeClass

Public Sub New()
End Sub

Public Sub New(ByVal i As Integer)
End Sub

Public Sub New(ByVal s As String)
End Sub

Public Sub New(ByVal ParamArray args() As String)
End Sub

End Class

You can even include optional parameters, however I normally favor
overloading to the Optional keyword.

As I stated in my other post, overloading constructors, base classes &

other
OOP concepts are explained in Robin A. Reynolds-Haertle's book "OOP with
Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from MS Press covers this plus

the
rest of OOP in VB.NET, in a very straight forward manner.

OOP concepts in VB.NET is also covered in this section of MSDN.

http://msdn.microsoft.com/library/de...ithObjects.asp

Of course you are welcome to ask in the newsgroups also.

Hope this helps
Jay

"Stephen Martinelli" <st*****@johnstontrading.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
thanks for the help...just one more question....

can a class have more then two parameterized constructors?..i would
like
to
be able to instanciate the class with a different number of

argument.....
thanks folks

steve




Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.