Share via


How to: Locate an Element in an Array in Visual BasicĀ 

This example declares an array of String objects named zooAnimals, populates it, and then finds the element "turtle" and displays its location.

Example

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Visual Basic Language. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Public Sub findAnimal()
    Dim zooAnimals(2) As String
    zooAnimals(0) = "lion"
    zooAnimals(1) = "turtle"
    zooAnimals(2) = "ostrich"
    Dim turtleIndex As Integer
    turtleIndex = (Array.IndexOf(zooAnimals,"turtle"))
    MsgBox("The turtle is element " & turtleIndex)
End Sub

Compiling the Code

This example requires:

  • Access to Mscorlib.dll and the System namespace.

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Put a Value into an Array
How to: Get a Value from an Array
How to: Reverse the Contents of An Array in Visual Basic
How to: Sort An Array in Visual Basic
Troubleshooting Arrays

Reference

System.Array.IndexOf

Concepts

Overview of Arrays in Visual Basic

Other Resources

Arrays in Visual Basic