Overloads Problem
-
I'm teaching myself VB.NET and have come across a problem with either the tutorial book (Visual Basic .NET Programming with Peter Aitken, Chapter 9, page 191) or the software (Microsoft Visual Studio .NET). The problem I'm getting is after typing in code from example in book: Public Class UseOverloadedConstructor Private pName as String Public Overloads Sub New(ByVal Val As String) pName = Val End Sub Public Overloads Sub New() pName = "" End Sub Public Property Name() As String Get Name = pName End Get Set(ByVal Value As String) pName = Value End Set End Property End Class I get an error in the build. Saying that Overloads don't work with New! What is the problem? What am I missing? Can anyone help? Thank you :confused:
-
I'm teaching myself VB.NET and have come across a problem with either the tutorial book (Visual Basic .NET Programming with Peter Aitken, Chapter 9, page 191) or the software (Microsoft Visual Studio .NET). The problem I'm getting is after typing in code from example in book: Public Class UseOverloadedConstructor Private pName as String Public Overloads Sub New(ByVal Val As String) pName = Val End Sub Public Overloads Sub New() pName = "" End Sub Public Property Name() As String Get Name = pName End Get Set(ByVal Value As String) pName = Value End Set End Property End Class I get an error in the build. Saying that Overloads don't work with New! What is the problem? What am I missing? Can anyone help? Thank you :confused:
you cant use New as an overloads sub , change the name of the sub : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MyNew("my string") '/// set the text to the name MsgBox(TheName) '/// check TheName now contains the correct info End Sub Private pName As String Public Overloads Sub MyNew(ByVal Val As String) pName = Val End Sub Public Overloads Sub MyNew() pName = "" End Sub Public Property TheName() As String Get TheName = pName End Get Set(ByVal Value As String) pName = Value End Set End Property switch(twinsOnWay) { case ("twins on the way"): MessageBox.Show("for mr and mrs dynamic","twins on the way"); break;
-
I'm teaching myself VB.NET and have come across a problem with either the tutorial book (Visual Basic .NET Programming with Peter Aitken, Chapter 9, page 191) or the software (Microsoft Visual Studio .NET). The problem I'm getting is after typing in code from example in book: Public Class UseOverloadedConstructor Private pName as String Public Overloads Sub New(ByVal Val As String) pName = Val End Sub Public Overloads Sub New() pName = "" End Sub Public Property Name() As String Get Name = pName End Get Set(ByVal Value As String) pName = Value End Set End Property End Class I get an error in the build. Saying that Overloads don't work with New! What is the problem? What am I missing? Can anyone help? Thank you :confused:
Constructors don't need to be explicitly overloaded like other methods. You can simple create all of the New methods (constructors) you want, as long as they have unique signatures. If you rename the method to something like MyNew, it becomes an instance method and will not be used as a constructor.
-
you cant use New as an overloads sub , change the name of the sub : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MyNew("my string") '/// set the text to the name MsgBox(TheName) '/// check TheName now contains the correct info End Sub Private pName As String Public Overloads Sub MyNew(ByVal Val As String) pName = Val End Sub Public Overloads Sub MyNew() pName = "" End Sub Public Property TheName() As String Get TheName = pName End Get Set(ByVal Value As String) pName = Value End Set End Property switch(twinsOnWay) { case ("twins on the way"): MessageBox.Show("for mr and mrs dynamic","twins on the way"); break;
-
Constructors don't need to be explicitly overloaded like other methods. You can simple create all of the New methods (constructors) you want, as long as they have unique signatures. If you rename the method to something like MyNew, it becomes an instance method and will not be used as a constructor.