Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the IndexOf() method of Array class
By Nidhi Last Updated : November 11, 2024
Array.IndexOf() Method in VB.Net
The IndexOf() method is used to search the item within the array and return the index of the first occurrence of the item.
Syntax
Function IndexOf (ByVal arr() as object, Byval item as integer) as Integer
Parameter(s)
- arr: It is the specified integer array.
- Item: Item to be searched within the array.
Return Value
It returns the index of the first occurrence of the item within the array.
VB.Net code to demonstrate the example of Array.IndexOf() method
The source code to demonstrate the IndexOf() method of the Array class is given below. The given program is compiled and executed successfully.
'VB.NET program to demonstrate the IndexOf()
'method of Array class.
Imports System
Module Module1
Sub Main()
Dim arr() As Integer = {10, 20, 30, 40, 30}
Dim index As Integer
'Search the specified item in the array and return
'the index of the first occurrence of an item.
index = Array.IndexOf(arr, 30)
Console.WriteLine("Index of item {0} is: {1}", 30, index)
Console.WriteLine()
End Sub
End Module
Output
Index of item 30 is: 2
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
In the Main() function, we created an array arr1. Then we get the index of the first occurrence of the item within the array using IndexOf() method of the Array class. After that, we printed the index of the item on the console screen.
VB.Net Array Programs »