Name
Array.IndexOf Method
Class
System.Array
Syntax
Array.IndexOf(Array, Value[, startIndex[, count]]
)
-
Array
Use: Required
Data Type: Any array
The array to be searched
-
Value
Use: Required
Data Type: Any
The object that is searched for
-
startIndex
Use: Optional
Data Type: Integer
The index at which to start the search
-
count
Use: Optional
Data Type: Integer
The number of items to search
Return Value
The index of the first occurrence of Value
in Array
, or -1
Description
Returns an Integer representing the index of the first occurrence of
object
in Array
Rules at a Glance
Array
must be a one-dimensional array.By default, the IndexOf method searches for
Value
from the beginning to the end ofArray
.If
startIndex
is provided withoutcount
, IndexOf searches fromstartIndex
to the last element ofArray
.If both
startIndex
andcount
are provided, the method searchescount
elements starting atstartIndex
. In other words, it searches fromarray(startIndex)
toarray(startIndex + count
- 1)
.If
startIndex
is present and is outside of the range of the elements in array, the method returns -1.If
count
is present andstartIndex + count
- 1
exceeds the total number of elements inarray
, the method call generates an ArgumentOutOfRangeException exception.
Example
The following code searches for a value in an Integer array:
Dim i As Integer Dim a(99999) As Integer For i = 0 To 99999 a(i) = CInt(Rnd( ) * 100000) Next MsgBox(Array.IndexOf(a, 36500))
You can also specify the starting index for the search, as well as ...
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.