Array.LastIndexOf Method

   
Array.LastIndexOf Method

Class

System.Array

Syntax

 Array.LastIndexOf(   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 elements to search

Return Value

An Integer containing the index of the last occurrence of Object in Array

Description

Returns the index of the last occurrence of Object in Array

Rules at a Glance

  • Array must be a one-dimensional array.

  • The LastIndexOf method has the same syntax as the IndexOf method and works the same way as IndexOf, except that it searches from the end of the array and returns the largest index of a matching element.

  • By default, the LastIndexOf method searches for Value from the end to the beginning of Array .

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

  • If both startIndex and count are provided, the method searches count elements backward 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 , 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(100000) As Integer For i = 0 To 99999     a(i) = CInt(Rnd(  ) * 100000) Next MsgBox(Array.LastIndexOf(a, 36500)) 

You can also specify the starting index for the search, as well as the number of elements to search. For example:

 Array.LastIndexOf(array:=a, value:=136500, startIndex:=100, _                   count:=50) 

See Also

Array.IndexOf Method

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net