Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
Brandon Avatar asked Mar 25 '13 02:03

Brandon


2 Answers

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
like image 171
Iswanto San Avatar answered Sep 30 '22 05:09

Iswanto San


According to MSDN in VB.NET, you can use the Option Compare Statement by simply adding 1 line of code to your file:

Option Compare Text

If you add the above line to the beginning of your code you are telling the CLR to switch from default (Option Compare Binary) to the case-insensitive comparison as the new default for the = operator.

I don't know if there is any C# alternative.

like image 26
beppe9000 Avatar answered Sep 30 '22 05:09

beppe9000