?
Solved

VB  conversion error

Posted on 2011-03-13
2
Medium Priority
?
460 Views
Last Modified: 2012-05-11
I have this code below & I am gettin this error:

Option Strict On disallows implicit conversions from 'String' to 'Double'.

I get it on the first 2 lines that have the If statement.

Here is the VB code:


Dim intDirection As Integer
        Dim intDirNACheck As Integer
        Dim intResult As Integer
        intDirection = CInt(getAltitudeWeather(strAlt))

        ' If the first two characters are blanks, return "N/A" 
        intDirNACheck = CInt(getAltitudeWeather(strAlt).Substring(0, 2))
        If intDirNACheck = "" Then
            strDirResult = "N/A"
            Return strDirResult
        Else
            intDirNACheck = CInt(getAltitudeWeather(strAlt).Substring(0, 4))
            If intDirNACheck = "9900" Then
                strDirResult = "Calm"
                Return strDirResult
            Else
                intDirNACheck = CInt(getAltitudeWeather(strAlt).Substring(0, 2))
                If intDirNACheck > 36 Then
                    intResult = CInt(intDirNACheck - 50)
                    strDirResult = CStr(intResult)
                    Return strDirResult
                Else
                    Return strDirResult
                End If

            End If
        End If

Open in new window

0
Comment
Question by:someITGuy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 700 total points
ID: 35125664
Try this....

        Dim intDirection As Integer
        Dim strDirNACheck As String
        Dim intResult As Integer
        intDirection = CInt(getAltitudeWeather(strAlt))

        ' If the first two characters are blanks, return "N/A"
        strDirNACheck = getAltitudeWeather(strAlt).Substring(0, 2)
        If strDirNACheck = "" Then
            strDirResult = "N/A"
            Return strDirResult
        Else
            strDirNACheck = getAltitudeWeather(strAlt).Substring(0, 4)
            If strDirNACheck = "9900" Then
                strDirResult = "Calm"
                Return strDirResult
            Else
                strDirNACheck = getAltitudeWeather(strAlt).Substring(0, 2)
                If CInt(strDirNACheck) > 36 Then
                    intResult = CInt(strDirNACheck) - 50
                    strDirResult = CStr(intResult)
                    Return strDirResult
                Else
                    Return strDirResult
                End If

            End If
        End If

Open in new window


Wayne
0
 
LVL 2

Assisted Solution

by:dlmichener
dlmichener earned 300 total points
ID: 35149856
The "Option Strict On" statement requires that your code does not do implicit data conversions.  If the VB compiler determines that your code is trying to do implicit data conversions you will get this error message.

In your original example the IF statements are doing just that.  The IF statment is trying to implicitly convert a number (intDirNACheck) to a string (the "").

Wayne's code above is a much better approach as it always trys to compare like values (strings to strings, numbers to numbers).

If you had set "Option Strict" to OFF, you would not get the compile error that you are getting now.  The code would run, but depending upon the values returned from the getAltitudeWeather function you may get a run-time with the right/wrong value.

In general, it is a better practice to have "Option Strict" set to ON and catch these types of errors earlier in the development process than to have to fix it later on in the field.
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

752 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