Method
Arrays.IndexOf
Description
Used to search for a value in an array. Returns the index of the array element that contains the matching value.
Usage
result=array.IndexOf(TargetValue [,StartingIndex])
Part |
Type |
Description |
---|---|---|
result |
Index of the array element that contains the value TargetValue. |
|
array |
Any valid data type |
The array that you want to search. |
TargetValue |
Same type as array |
The value in the array that you want to find. It must be of the same data type as the array. The IndexOf function is not case-sensitive, but it is encoding-sensitive. |
StartingIndex |
Optional. Array element to begin the search. |
Notes
If the targetValue passed is not found, IndexOf returns -1.
IndexOf is not case-sensitive, but it is encoding-sensitive.
Arrays can have a maximum index value of 2,147,483,646.
Sample code
This example uses a ComboBox that has five items in its pop-up menu that have the names of the workdays of the week. The selected day is in its Text property. This example returns the index of the array element that matches the menu selection.
Var days() As String
days() = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
Var dayNumber As Integer
dayNumber = days.IndexOf(ComboBox1.Text)
If dayNumber = -1 Then
MessageBox("You didn't enter the name of any weekday.")
Else
MessageBox("You entered day number " + dayNumber.ToString)
End If
Compatibility
All project types on all supported operating systems.
See also
Arrays parent class; Var statement; Arrays concept for a complete list of functions; ParamArray keyword; Arrays concept