Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the LastIndexOf() method of Array class
By Nidhi Last Updated : November 11, 2024
Array.LastIndexOf() Method in VB.Net
The LastIndexOf() method is used to search the item within the array and return the index of the last occurrence of the item.
Syntax
Function LastIndexOf (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 last occurrence of the item within the array.
VB.Net code to demonstrate the example of Array.LastIndexOf() method
The source code to demonstrate the LastIndexOf() method of the Array class is given below. The given program is compiled and executed successfully.
'VB.NET program to demonstrate the LastIndexOf()
'method of Array class.
Imports System
Module Module1
Sub Main()
Dim arr() As Integer = {10, 20, 30, 20, 30}
Dim index As Integer
'Search the specified item in the array and return
'the index of the last occurrence of an item.
index = Array.LastIndexOf(arr, 20)
Console.WriteLine("Index of last occurrence of item {0} is: {1}", 20, index)
Console.WriteLine()
End Sub
End Module
Output
Index of last occurrence of item 20 is: 3
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 last occurrence of the item within the array using LastIndexOf() method of the Array class. After that, we printed the index of the item on the console screen.
VB.Net Array Programs »