Use of IndexOf-Array in VB.NET

To locate the index of a String, and then continue locating further instances we can accomplish this with a IndexOf method.
  • 5252

Arrays are using for store similar data types grouping as a single unit, the items in an array are indexed by their position in the array, numbered beginning with 0 (zero) for the first element in the array and the upper bound of an array is generally language and possibly system specific. We can access Array elements by its numeric index. To access array elements by its index we use IndexOf String Function which is available in the .NET Framework's base class library, using this you can locate the any index of an array.

Sometimes you need to locate the first index of a String, and then continue locating further instances. You can accomplish this with a Do While construct and the IndexOf method.

objects-tenElementArray.gif

In the following program , we declare an Array "values" capability of  five integer values and. Next step is to retrieve the elements of the Array using a For loop. For finding the index of first and third index of the values we used the IndexOf function of Array.

Example

Imports System
 
Public Class Main
 
    Shared Sub Main(ByVal args As String())
        Dim values(5) As Integer
        For i As Integer = 0 To 5
            values(i) = i
        Next i
 
        Console.WriteLine("Index of first value is :")
        Console.WriteLine(Array.IndexOf(values, 1).ToString)
        Console.WriteLine("Index of third value is :")
        Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
        Console.ReadLine()
    End Sub
 
End Class

Output

array.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.