VB.net If with declaration

Murray Brown
Murray Brown used Ask the Experts™
on
I ant to declare a variable in the following code used with an API but
I get an error showing that the variable is hidden in an encclosong blocck


If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
                Dim myxeroconsumer As New Consumer("xxx", "yyyy")
            Else
                Dim myxeroconsumer As New Consumer("gggg", "jjjjj")
            End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Murray,

Try this code:
   Dim myxeroconsumer As Consumer = Nothing
        If System.Web.Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
            myxeroconsumer = New Consumer("xxx", "yyyy")
        Else
            myxeroconsumer = New Consumer("gggg", "jjjjj")
        End If

Open in new window


Regards,
Chinmay.
Ryan ChongSoftware Team Lead
Commented:
you can try define your object as Object first:

Dim myxeroconsumer As Object
        If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
            myxeroconsumer = New Consumer("xxx", "yyyy")
        Else
            myxeroconsumer = New Consumer("gggg", "jjjjj")
        End If

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start Today