Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Overloads Problem

Overloads Problem

Scheduled Pinned Locked Moved Visual Basic
helpcsharptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrEasy
    wrote on last edited by
    #1

    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:

    D C 2 Replies Last reply
    0
    • M MrEasy

      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:

      D Offline
      D Offline
      dynamic
      wrote on last edited by
      #2

      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;

      M 1 Reply Last reply
      0
      • M MrEasy

        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:

        C Offline
        C Offline
        CBoland
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • D dynamic

          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;

          M Offline
          M Offline
          MrEasy
          wrote on last edited by
          #4

          Thank you for your help. I appreciate it very much. Congratulations on your new additions to your family too!

          1 Reply Last reply
          0
          • C CBoland

            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.

            M Offline
            M Offline
            MrEasy
            wrote on last edited by
            #5

            Thank you very much for helping me. You have given a better look at the way Contructors work. I just wish the book I bought had been better informative. I guess you get what you pay for. Thanks again!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups