Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

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
0
LeighWardle
Asked:
LeighWardle
1 Solution
 
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 JacksonCommented:
try :

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

...

Open in new window

0
 
LeighWardleAuthor Commented:
Thanks, sethi.
Regards,
Leigh
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now