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

constructors

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.