Solved - compilation error when trying to call overloaded constructor

Posted on 2014-08-25
Last Modified: 2014-08-26
Hi Experts,

Here is a sample of my code.

Public Class clsLicenceSentinel

    Private mvarintProgramNumber As Integer

    Public Sub New(intProgramNumber As Integer)

        mvarintProgramNumber = intProgramNumber

    End Sub

    Public Sub New(strProgramNameAndMajorVersion As String)

        Dim intProgramNumber As Integer

        Select Case strProgramNameAndMajorVersion
            Case "APSDS3"
                intProgramNumber = 1
            Case "CIRCLY6"
                intProgramNumber = 2
            Case "CIRCLY3"
                intProgramNumber = 3
            Case "CIRCLY4"
                intProgramNumber = 4
            Case "APSDS4"
                intProgramNumber = 5
            Case "CIRCLY5"
                intProgramNumber = 6
            Case "APSDS5"
                intProgramNumber = 7
            Case "HIPAVE1", "HIPAVE5" '20050208
                intProgramNumber = 8

        End Select

        'call the constructor 
        Call New (intProgramNumber)  '<<<< gives "Type Expected" error

    End Sub

End Class

Open in new window

I am trying to call the overloaded constructor (see 3rd statement from the end).
But this gives a compiler "Type Expected" error.

Question by:LeighWardle
    LVL 18

    Accepted Solution

    You can't call the constructor as its called by object itself at the time of creation. That's why you are getting this error.
    LVL 29

    Expert Comment

    by:Paul Jackson
    try :

    'call the constructor 

    Open in new window

    LVL 1

    Author Closing Comment

    Thanks, sethi.
    LVL 1

    Author Comment

    Thanks, Paul Jackson, for your suggestion.

    I tried that, but it gave this error:

    Constructor call is valid only as the first statement in an instance constructor.

    I have now found a workaround to get my code working - by putting all the code that is common to the two contructors into a single sub.
    LVL 18

    Expert Comment

    Glad I could help. Thanks.
    LVL 15

    Expert Comment

    Leigh, that method is the correct way to do it, its not a work around.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now