LastIndexOf
Description
The LastIndexOf() method method returns the last index at which a given element can be found in the array, or -9999 if it is not present.
Syntax
expression.LastIndexOf(SearchElement,[FromIndex],[CompType])
Parameters
- Name
SearchElement- Type
Variant- Necessity
- Required
- Description
- The element to locate in the array.
- Name
FromIndex- Type
Long- Necessity
- Optional
- Description
- The index at which to begin searching backwards. If omitted, search starts at the array’s
UpperBound. If a value aboveUpperBoundis provided, it is clamped toUpperBound. If the provided index is lower thanLowerBoundand negative, it is treated as an offset from the end (LowerBound + Length + FromIndex) and then clamped toLowerBoundif needed.
- Name
CompType- Type
ComparisonType (Long)- Necessity
- Optional
- Description
- The type of comparison to perform. See the comparison type enumeration. Default is CT_EQUALITY.
ComparisonType Enumerations
CT_EQUALITY- Compares
SearchElementagainst the element at the current index for equality. This is the default comparison method. CT_LIKENESSSearchElementis treated as a string pattern and compared against the element as the current index using theLikeoperator. If this option is chosenSearchElementmust be a String type or an error will be raised.
Returns
- Type
Long- Description
- The last index of the element in the array; -9999 if not found.
Example
Public Sub LastIndexOfExample()
Dim result As Long
Dim MyArray As BetterArray
Set MyArray = New BetterArray
MyArray.Push "Apple", "Banana", "Orange", "Apple", "Mango"
result = MyArray.LastIndexOf("Apple")
' expected output:
' result = 3
End Sub