-3

My code below doesn't work:

 For i As Integer = 0 To ListBox1.Items.Count - 1
    If ListBox1.Items(i).ToString = name 
       And ListBox2.Items(i).ToString = founds 
       And ListBox3.Items(i).ToString = foundss Then
             found = (i)
             found1 = found.ToString.Contains(name, StringComparer.OrdinalIgnoreCase) <> 1
    End If
 Next
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    One basic way is just to convert all your strings to lowercase before you compare them. – ADyson Sep 21 '19 at 10:48
  • I'm new to visual basic. Could you please show me how to do that ? – Tirso Valderrama Colon Jr. Sep 21 '19 at 10:50
  • 2
    There's a function called ToLower. You can find it in the Microsoft documentation, or in the intellisense in visual studio. And you can probably find a lot of existing examples of usage online. Just search a bit – ADyson Sep 21 '19 at 10:55
  • 2
    How does this line make any sense in that context: `found = (i)`? How is the value of your loop counter ever going to contain anything that requires case-insensitivity when it's a number? – jmcilhinney Sep 21 '19 at 16:07
  • @ADyson, that's bad advice. Don't use `ToUpper` or `ToLower` to simulate case-insensitivity because it doesn't always work as expected. There's no need anyway, given that `String.IndexOf` and `String.Equals` both support case-insensitivity. – jmcilhinney Sep 21 '19 at 16:09

2 Answers2

1

You are confusing String.Contains with String.IndexOf. Compare returns a Boolean, not an Integer, and it doesn't support case-insensitivity. IndexOf is the one that returns an Integer and supports case-insensitivity. Also, it's StringComparison, not StringComparer. Finally, -1 is the result that indicates no match:

found1 = found.ToString().IndexOf(name, StringComparer.OrdinalIgnoreCase) <> -1

As a bonus, here's an extension method that will let you call a Contains method that still returns a Boolean but also supports case-insensitivity:

Imports System.Runtime.CompilerServices

Public Module StringExtensions

    <Extension>
    Public Function Contains(source As String, value As String, comparisonType As StringComparison) As Boolean
        Return source.IndexOf(value, comparisonType) <> -1
    End Function

End Module

E.g.

found1 = found.ToString().Contains(name, StringComparer.OrdinalIgnoreCase)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

It's not clear what exactly your code does. But the String.Contains method returns Boolean and not Integer. So you probably need to change your call to it:

found1 = found.ToString.Contains(name, StringComparer.OrdinalIgnoreCase)

And to make your other tests case-insensitive, replace the "=" operator with the String.Equals method. Something like:

ListBox1.Items(i).ToString().Equals(name, StringComparer.OrdinalIgnoreCase)
Peter Macej
  • 4,831
  • 22
  • 49