constructors

  • Thread starter Thread starter Stephen Martinelli
  • Start date Start date
S

Stephen Martinelli

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
 
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/d...us/vbcn7/html/vbconProgrammingWithObjects.asp

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

Hope this helps
Jay
 
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
 
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.
 
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.
 
* "Scott M. said:
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.
 
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
 
Back
Top