Solved

VB  conversion error

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

733 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