Solved

VB  conversion error

Posted on 2011-03-13
2
433 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
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 175 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 75 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

10 Experts available now in Live!

Get 1:1 Help Now