Difference between revisions of "IndexOf"

From Xojo Documentation

(Sample Code)
 
Line 43: Line 43:
  
 
== Sample Code ==
 
== 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.
+
This example uses a [[DesktopComboBox|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.
 
<rbcode>
 
<rbcode>
 
Var days() As String
 
Var days() As String
 
days() = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
 
days() = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
 
Var dayNumber As Integer
 
Var dayNumber As Integer
dayNumber = days.IndexOf(ComboBox1.Value)
+
dayNumber = days.IndexOf(ComboBox1.Text)
 
If dayNumber = -1 Then
 
If dayNumber = -1 Then
 
   MessageBox("You didn't enter the name of any weekday.")
 
   MessageBox("You didn't enter the name of any weekday.")
 
Else
 
Else
   MessageBox("You entered day number " + Str(dayNumber))
+
   MessageBox("You entered day number " + dayNumber.ToString)
 
End If
 
End If
 
</rbcode>
 
</rbcode>

Latest revision as of 20:48, 13 February 2022

Method

Arrays.IndexOf(targetValue As ArrayDataType, Optional startingIndex As Integer = 0) As Integer

Supported for all project types and targets.

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.

Usage

result=array.IndexOf(TargetValue [,StartingIndex])

Part Type Description
result Integer Index of the array element that contains the value TargetValue.

If no match is found, result is set to -1.

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 Integer Optional. Array element to begin the search.

If omitted, the search starts at the first array element. If you specify an array element outside the range of elements in array, an OutOfBoundsException is raised.

Notes

IndexOf is not case-sensitive, but it is encoding-sensitive.

All arrays are indexed starting at position 0. 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

See Also

Var statement; Arrays concept for a complete list of functions; ParamArray keyword; Arrays concept