vb.net - compilation error when trying to call overloaded constructor

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.

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?
 
SethiCommented:
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.
0
 
Paul JacksonSoftware EngineerCommented:
try :

...
'call the constructor 
        Me.New(intProgramNumber)

...

Open in new window

0
 
LeighWardleAuthor Commented:
Thanks, sethi.
Regards,
Leigh
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LeighWardleAuthor Commented:
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.
0
 
SethiCommented:
Glad I could help. Thanks.
0
 
ChloesDadCommented:
Leigh, that method is the correct way to do it, its not a work around.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.