Reputation: 3
I am learning visual basic online, however we just got a teacher switch and they have not replied to my question. So I figured I might as well ask it here. We where asked to make a computer troubleshooting program that only uses text boxes, labels, and a button. I wrote this code but when I run it I get this error "Conversion from string "Y" to type 'Boolean' is not valid." and I have no idea why or how to fix it. Thanks for the help.
Private Sub btnHelp_Click(sender As Object, e As System.EventArgs) Handles btnHelp.Click
Dim strBeep As String
Dim strHDD As String
strBeep = Me.txtBeep.Text
strHDD = Me.txtHDD.Text
If strBeep And strHDD = "Y" Then
Me.lblMessage.Text = "Contact tech support."
ElseIf strBeep = "Y" And strHDD = "N" Then
Me.lblMessage.Text = "Check drive contacts."
ElseIf strBeep And strHDD = "N" Then
Me.lblMessage.Text = "Bring computer to repair center."
ElseIf strBeep = "N" And strHDD = "Y" Then
Me.lblMessage.Text = "Check the speaker connections."
End If
End Sub
End Class
Upvotes: 0
Views: 3274
Reputation: 56
A cleaner way would be to convert the strings from the text boxes at the earliest opportunity. So rather than having Dim strBeep As String, have:
Dim bBeep As Boolean
Dim bHDD As Boolean
bBeep = (Me.txtBeep.Text = "Y")
bHDD = (Me.txtHDD.Text = "Y")
Then your if statements look like this:
If bBeep And bHDD Then
'do something
Else If bBeep And Not bHDD
'do something else
End If
Upvotes: 1
Reputation: 180927
Your
If strBeep And strHDD = "Y" Then
should be
If strBeep = "Y" And strHDD = "Y" Then
And
is a boolean operator and it's evaluated after the comparison strHDD = "Y"
has resulted in true or false. In other words, what you're writing does not mean "if both are Y", it means if strBeep is true and strHDD is "Y" which to the compiler makes no sense since strBeep is not a true or false value.
Upvotes: 0
Reputation: 204766
You are using strBeep
in an if condition without comparing it to something. This is a text and it can't be converted to boolean
.
Do this:
if strBeep = "Y" ...
instead of
if strBeep ...
Complete if conditions:
If strBeep = "Y" And strHDD = "Y" Then
Me.lblMessage.Text = "Contact tech support."
ElseIf strBeep = "Y" And strHDD = "N" Then
Me.lblMessage.Text = "Check drive contacts."
ElseIf strBeep = "N" And strHDD = "N" Then
Me.lblMessage.Text = "Bring computer to repair center."
ElseIf strBeep = "N" And strHDD = "Y" Then
Me.lblMessage.Text = "Check the speaker connections."
End If
Upvotes: 0