[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB  conversion error

Posted on 2011-03-13
2
Medium Priority
?
468 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 48

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 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