0

So im trying to get this code to work but its not returning any values for the If Like portion of the code. I have no idea why this is happening.

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    'display color button click event procedure should display the color of the item
    'whos item number is entered by the user.

    'all item numbers contain exactly seven characters
    'all items are available in four colors: blue, green, red, and white.
    'the fourth character in the item number indicates the items color
    'as follows: B or b indicates blue etc
    'if the item number does not contain 7 charactors OR
    'if the forth character is not one of the valid color characters,
    'the procedure should display the appropriate message

    If txtItem.Text Like "###[bBgGrRrwW]###" Then
        If txtItem.Text.Contains("bB") Then
            lblColor.Text = "Blue"
        ElseIf txtItem.Text.Contains("gG") Then
            lblColor.Text = "Green"
        ElseIf txtItem.Text.Contains("rR") Then
            lblColor.Text = "Red"
        ElseIf txtItem.Text.Contains("Ww") Then
            lblColor.Text = "White"
        End If
    ElseIf txtItem.Text IsNot "###[bBgGrRwW]###" Then
        MessageBox.Show("Bad Job", "Color Project", MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Information)
    End If
End Sub
0000
  • 677
  • 2
  • 8
  • 20
  • What is the value in `txtItem.Text`? – Steve Mar 26 '14 at 20:38
  • Make sure you understand [what `Like` does](http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx), [what `IsNot` does](http://msdn.microsoft.com/en-us/library/t3bat82c.aspx) and [what `Contains` does](http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx). – GSerg Mar 26 '14 at 20:39
  • the value of txtItem.Text is what the user enters into the text box. I'm trying to validate the text with the like statement. To the best of my knowledge this should work. – 0000 Mar 26 '14 at 20:42

1 Answers1

4

Where you have .Contains("bB"), you are asking it to check if the string contains bB, i.e. two characters. What you actually want to do is check if it contains B and ignore the case of the character. Although Contains does not have a case-insensitive option, IndexOf does: String.IndexOf Method (String, StringComparison), so you could use

If txtItem.Text.IndexOf("B", StringComparison.OrdinalIgnoreCase) >= 0 Then
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Or you could do `If txtItem.Text.ToLower.Contains("b") Then` I find it easier to read – Pacane Mar 26 '14 at 20:48
  • @Pacane, that does not work in all situations (cultures), so using the overload of the `IndexOf` method is safer. See the [Turkey Problem](http://blog.codinghorror.com/whats-wrong-with-turkey/) for more info. – Steven Doggart Mar 26 '14 at 20:52
  • @StevenDoggart Oh well, even the solution is provided in your link! `.ToLower(System.Globalization.CultureInfo.InvariantCulture)`... haha this is insane though. – Pacane Mar 26 '14 at 21:25
  • @Pacane You may like [Jon Skeet and Tony the Pony on Vimeo](http://vimeo.com/7403673) for some more comments on globalisation issues. – Andrew Morton Mar 26 '14 at 22:13
  • @AndrewMorton That was awesome. But my favorite remains http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result – Pacane Mar 27 '14 at 00:06