Results 1 to 2 of 2

Thread: New Constructor calling a base

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    New Constructor calling a base

    Why is this not legal?

    VB Code:
    1. Private x as String
    2. Private y as String
    3.  
    4. Public Sub New(ByVal strParam As String)
    5. x = strParam
    6. End Sub
    7.  
    8. Public Sub New(ByVal strParam As String, ByVal strParam2 As String)
    9. Try
    10. MyClass.New(strParam)
    11. y = strParam2
    12. Catch e as exception
    13. ....
    14. End Try

    I can't put the MyClass.New call inside of the Try block? (It works fine outside).

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't know why but you can only call the BaseClass constructor from the first executable code line in the current class constructor. Kind of funky if you ask me but that is the way it is.
    VB Code:
    1. 'works
    2. Public Sub New(ByVal strParam As String, ByVal strParam2 As String)
    3.   MyBase.New(strParam)
    4.  
    5. 'works
    6. Public Sub New(ByVal strParam As String, ByVal strParam2 As String)
    7. 'comment first since its non-executable
    8.    MyBase.New(strParam)
    9.  
    10. 'does not work
    11. Public Sub New(ByVal strParam As String, ByVal strParam2 As String)
    12.    Msgbox("Created") 'this is executable or whatever you want to call it so you can not call the base constructor after that
    13.    MyBase.New(strParam)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width