Brandon Brandon - 6 months ago 24
Vb.net Question

Case insensitive

If TextBox2.Text = "a" AndAlso TextBox21.Text = "a" Then
'MessageBox.Show("A")
totCorrect = totCorrect + corAns
ElseIf TextBox2.Text = "b" AndAlso TextBox21.Text = "b" Then
'MessageBox.Show("B")
totCorrect = totCorrect + corAns
ElseIf TextBox2.Text = "c" AndAlso TextBox21.Text = "c" Then
'MessageBox.Show("C")
totCorrect = totCorrect + corAns
ElseIf TextBox2.Text = "d" AndAlso TextBox21.Text = "d" Then
'MessageBox.Show("D")
totCorrect = totCorrect + corAns
Else
totWrong = totWrong + wrgAns
Label13.Visible = True
End If


I am trying to make the letters a,b,c,d that the user enters insensitive. Tried to use the UCase, but it did not work (not sure if I am using it wrong). I am in Visual Studio 2012 and using VB. Any references would be great.

Answer

You can use String.Compare method : String.Compare (String strA, String strB, Boolean ignoreCase)

Pass ignoreCase argument with true will perform case insensitive comparison.

If String.Compare(TextBox2.Text, "a", true) = 0 AndAlso String.Compare(TextBox21.Text, "a", true) = 0 Then
        'MessageBox.Show("A")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "b", true) = 0 AndAlso String.Compare(TextBox21.Text, "b", true) = 0 Then
        'MessageBox.Show("B")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "c", true) = 0 AndAlso String.Compare(TextBox21.Text, "c", true) = 0 Then
        'MessageBox.Show("C")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "d", true) = 0 AndAlso String.Compare(TextBox21.Text, "d", true) = 0 Then
        'MessageBox.Show("D")
        totCorrect = totCorrect + corAns
    Else
        totWrong = totWrong + wrgAns
        Label13.Visible = True
    End If

Another idea is to uppercase or lowercase the input using ToUpper or ToLower.

If TextBox2.Text.ToUpper() = "A" AndAlso TextBox21.Text.ToUpper() = "A" Then
            'MessageBox.Show("A")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "B" AndAlso TextBox21.Text.ToUpper() = "B" Then
            'MessageBox.Show("B")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "C" AndAlso TextBox21.Text.ToUpper() = "C" Then
            'MessageBox.Show("C")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "D" AndAlso TextBox21.Text.ToUpper() = "D" Then
            'MessageBox.Show("D")
            totCorrect = totCorrect + corAns
        Else
            totWrong = totWrong + wrgAns
            Label13.Visible = True
        End If
Comments