Hello,
Im trying to make a program which basically performs unit conversion.
I have basically made two text boxes, one for the S.I. Units and the other for British Units. The general idea is that when i input any number in the S.I. Unit text box, it should display the answer in British Unit text Box and similiarly if i input any number in British Unit Textbox, the answer should be displayed in S.I.Unit textbox. Now the problem lies here that the code that i created is by using if else. now the compiler automatically gives me the correct result in the British box when i input the value in the s.i. unit box, but when i input the value in the british text box, it gives an error that :
"An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "" to type 'Double' is not valid."

Heres the part of the code.

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim p As String = "Pressure"
        Dim d As String = "Density"
        Dim t As String = "Temperature"
        Dim f As String = "Force"
        Dim si As Double = siubox.Text
        Dim pi As Double = bubox.Text
        Dim a As Double = 0.0208865 'for pressure si to british
        Dim b As Double = 47.73 'for pressure british to si
        While ComboBox1.Text = p
            If siubox.Text = si Then
                bubox.Text = a * si
            ElseIf bubox.Text = pi Then
                siubox.Text = pi * b

            End If
            Exit While

        End While

    End Sub

Will appreciate the help. Thanks.

Recommended Answers

All 4 Replies

Your if statement is wrong. siubox.text will always equal to si. Because you declare si to equal to siubox.text
so it will always calculate bubox.text=a * si

try an if statement of

if siubox.text<>"" then
si=cdbl(siubox.text)
bubox.text=a * si
else
b=cdbl(bubox.text)
siubox.text=pi * b
end if

Thanks! That removed the error, but now, it shows the value of si unit 0 everytime i enter any british unit

oh i found the problem. lol i wrote the variable p instead of pi. Thanks alot :D

Mark as solved....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.