Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implicit Conversion Problem for String to Double

Status
Not open for further replies.

larrydavid

Programmer
Jul 22, 2010
174
0
0
US
Hello,

I have a VB.NET function I'm using to pass values, in this case simple addition to test. Here is a small class with the functions:

Code:
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

    Private Function GetResult(ByVal firstNumber As String, ByVal secondNumber As String, ByVal [function] As Integer) As String
        Dim a As Integer, b As Integer
        Dim result As String = Nothing

        TextBox1.Text = ""
        TextBox2.Text = ""

        If Not Integer.TryParse(firstNumber, a) Then
            TextBox1.Text = "Must be a valid 32-bit integer!"
            Return ""
        End If

        If Not Integer.TryParse(secondNumber, b) Then
            TextBox2.Text = "Must be a valid 32-bit integer!"
            Return ""
        End If

        Try
            Select Case [function]
                Case 0
                    result = firstNumber + " + " + secondNumber + " = " + Add(a, b)
                    Exit Select
                    'Case 1
                    '    result = firstNumber + " - " + secondNumber + " = " + Subtract(a, b)
                    '    Exit Select
            End Select
        Catch e As Exception
            Label1.ForeColor = System.Drawing.Color.Red
            result = "Invalid result"
        End Try
        Return result
    End Function

    Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Label1.ForeColor = System.Drawing.Color.Black
        Label1.Text = GetResult(TextBox1.Text, TextBox2.Text, DropDownList1.SelectedIndex)
    End Sub
End Class

When I step through and debug this I get:
Invalid Cast Exception
Conversion from string "2 + 2 = " to type 'Double' is not valid.

Option Strict On disallows implicit conversions from 'String' to 'Double'.
So I tried this for Case 0 above:
result = CStr(CDbl(firstNumber + " - " + secondNumber + " = ") + Add(a, b))

Still no go.

I have no more hair left as I've already pulled all of it out. So, any help would be greatly appreciated.

Thanks,
Larry



 
In general, + performs arithmetic addition when possible, and concatenates only when both expressions are strings. Since your Add() function is returning an integer value, you'll have to add .toString() method to the call.

Code:
result = firstNumber + " + " + secondNumber + " = " + Add(a, b).ToString()

or use the "&" operator to widen the integer to a string :

Code:
result = firstNumber & " + " & secondNumber & " = " & Add(a, b)



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top