user4974730
user4974730

Reputation:

VB "Conversion from string "s" to type 'Boolean' is not valid."

If it matters I am using Visual Studio Express 2012 and Windows 7 Professional.

I have simple VB program which throws this exception:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Conversion from string "s" to type 'Boolean' is not valid.

This is troublesome portion of code:

If stroperation = "S" Or "s" Then

What have I done wrong?

Entire VB program:

Public Class MainForm
Public stroperation, strnumber1, strnumber2, strresult As String
Public decnumber1, decnumber2, decresult As Decimal
Public operation
Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
    operation = operationTextBox.Text
    stroperation = CStr(operation)
    strnumber1 = number1TextBox.Text
    decnumber1 = CDec(strnumber1)
    strnumber2 = number2TextBox.Text
    decnumber2 = CDec(strnumber2)
    If stroperation = "S" Or "s" Then
        decresult = decnumber1 - decnumber2
        resultLabel.Text = "Difference: " & strresult
    ElseIf stroperation = "A" Or "a" Then
        decresult = decnumber1 + decnumber2
        resultLabel.Text = "Sum: " & strresult
    Else
        MsgBox("Enter A, a, S, or S.")
    End If
End Sub
End Class

Upvotes: 0

Views: 1156

Answers (1)

rory.ap
rory.ap

Reputation: 35270

You need to change it to If stroperation = "S" Or stroperation = "s" Then

Or better yet, you could do this:

If stroperation.Equals("S", StringComparison.CurrentCultureIgnoreCase) Then

Upvotes: 2

Related Questions