O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Array.IndexOf Method

Class

System.Array

Syntax

Array.IndexOf(Array, Value[, startIndex[, count]])
Array (required; any array)

The array to be searched

Value (required; any)

The object that is searched for

startIndex (optional; Integer)

The index at which to start the search

count (optional; 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 value 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 of Array.

  • If startIndex is provided without count, IndexOf searches from startIndex to the last element of Array.

  • If both startIndex and count are provided, the method searches count elements starting at startIndex. In other words, it searches from array(startIndex) to array(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 and startIndex + count - 1 exceeds the total number of elements in Array, 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 the number of elements to search. For example: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required