Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,094 questions

40,775 answers

573 users

How to use Array.BinarySearch() method to search in Array of int objects in VB.NET

1 Answer

0 votes
Module Module1

    Sub Main()

        Dim intArray As Array = Array.CreateInstance(GetType(Int32), 5)

        intArray.SetValue(12, 0)
        intArray.SetValue(11, 1)
        intArray.SetValue(27, 2)
        intArray.SetValue(73, 3)
        intArray.SetValue(32, 4)

        Array.Sort(intArray)

        PrintArray(intArray)

        Dim o As Object = 27
        FindObject(intArray, o)

        o = 114
        FindObject(intArray, o)

    End Sub

    Public Sub PrintArray(arr As Array)
        Dim cols As Integer = arr.GetLength(arr.Rank - 1)
        For Each o As Object In arr
            Console.Write("  {0}", o)
        Next o
        Console.WriteLine()
    End Sub

    Public Sub FindObject(arr As Array, o As Object)
        Dim index As Integer = Array.BinarySearch(arr, o)

        If index < 0 Then
            Console.WriteLine("Object: {0} not found", o)
        Else
            Console.WriteLine("Object found: {0} at index {1}", o, index)
        End If
    End Sub

End Module

' run:
' 
'     11  12  27  32  73
' Object found:  27 at index 2
' Object :  114 Not found

 





answered Apr 9, 2016 by avibootz
...