2015-10-13 138 views
0

如果有問題,我正在使用Visual Studio Express 2012和Windows 7 Professional。VB「從字符串」s「轉換爲類型」布爾「無效。」

我有簡單的VB程序,拋出該異常:

「」類型的未處理的異常出現在Microsoft.VisualBasic.dll中

從字符串轉換「的」鍵入「布爾'無效。

這是代碼的麻煩部分:

If stroperation = "S" Or "s" Then 

我做了什麼錯?

整個VB程序:

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 

回答

3

您需要將其更改爲If stroperation = "S" Or stroperation = "s" Then

或者更好的是,你可以這樣做:

If stroperation.Equals("S", StringComparison.CurrentCultureIgnoreCase) Then 
相關問題