Constructor overloading in VB.NET

me_again

Newcomer
Joined
Mar 26, 2003
Messages
14
Location
Boksburg, South Africa
I think I'm missing something here...

In the following links
http://msdn.microsoft.com/msdnmag/issues/01/08/Instincts/default.aspx
http://msdn.microsoft.com/library/d...ry/en-us/dndotnet/html/overloadingmethods.asp
the authors discuss constructor overloading in VB.NET, using code examples such as
Visual Basic:
Class Person
  Private name As String
  Private age As Integer

  Overloads Public Sub New()
    MyClass.New("John Doe", -1)
  End Sub

  Overloads Public Sub New(n As String, a As Integer)
    name = n
    age = a
  End Sub
End Class

Did Microsoft make changes to the VB language after these articles were released?

The reason I'm asking is that for the life of me, I can't create overloaded constrcutors in VB.NET
When I try the code snippet above, I get 'Sub New can not be declared Overloads' (See the attachment)

Can someone please tell me why this is?
 

Attachments

Remove the Overloads modifier. BTW, they do mention how to add another constructor some where in the middle of your first url artical:
Unlike previous versions of Visual Basic, Visual Basic .NET does not support the Class_Initialize method. Instead, you add initialization support to your class by adding one or more constructors. You add a constructor to a class by adding a Sub procedure named New. Constructors are very flexible because each one can have its own custom parameter list. Here's an example.

Class Person
Private name As String
Private age As Integer

' parameterized constructor
Public Sub New(n As String, a As Integer)
name = n
age = a
End Sub
End Class

and your second url:
Overloaded Constructors
When you instantiate a new object, Visual Basic .NET calls an implicit New method. If you don't write the New method yourself, Visual Basic .NET builds one for you. If you wish to initialize any variables within an object at the time you create the new object, you can write an overloaded New method. You can then pass one or more values to this New method. You will probably find that overloading the New method is your most common use of overloading.

Open the Line.vb class module and add the code shown below.
Public Sub New()
' Do Nothing
End Sub

Public Sub New(ByVal Line As String)
mstrLine = Line
End Sub
 
RESOLVED - Constructor overloading in VB.NET

I've posted twice in this forum and answered my own posts both times. Maybe is shoul do a bit more research before I post :p

The answer to my question is to declare the Sub New's WITHOUT the Overloads keyword.
Visual Basic:
Class Person
  Private name As String
  Private age As Integer

  Public Sub New()
    MyClass.New("John Doe", -1)
  End Sub

  Public Sub New(ByVal n As String, ByVal a As Integer)
    name = n
    age = a
  End Sub
End Class
See http://www.vbip.com/books/1861007167/chapter_7167_07.asp
 
Back
Top