Solved

VB  conversion error

Posted on 2011-03-13
2
437 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

867 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

21 Experts available now in Live!

Get 1:1 Help Now