Search element in array

Hello my friends,
My question is really basic but I wanted to ask the opinion of real programmers.
I have a list of items.
Example :
Var People() As String = Array("John", "Sally", "Fred", "Nancy")
I know a method to check if an element belongs to the list, it is to loop to find out if it belongs to the list or not.

For i = 0 To People(i)
if Name = People(i) Then...

Or I could use a Dictionary or even a String with if Instr(People, Name) <> 0 Then…
Or I use Join to merge the words in the list into String and I use InStr.
I would like to work only with Arrays, isn’t there a clean way to find an element in an Array?
If Name in People Then....

Array.IndexOf should do the trick for you:

IndexOf(item As DataType, Optional startIndex As Integer) As Integer

Used to search for a value in an array. Returns the index of the array element that contains the matching value. Arrays are zero-based.

https://documentation.xojo.com/api/language/arrays.html#arrays-indexof

Returns the Index if found or -1 if not found.

2 Likes

Thank you very much Richard,
I should have read what’s new since 2010 on Arrays :slight_smile: for example.
BB