Results 1 to 7 of 7

Thread: [2005] Using Array.Exists?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    [2005] Using Array.Exists?

    I'm trying to use the Array.Exists function and having problems with it. I tried using the Help but it only shows examples in C++/C#. Here is what I have and what it's giving me back.

    Code:
                For Each row As DataRow In dsQVElements.Tables("tblQueueView").Rows
                    If arrQueueView Is Nothing Then
                        ReDim Preserve arrQueueView(x)
                        arrQueueView(x).groupID = row("groupID")
                        arrQueueView(x).labelName = row("labelName")
                        arrQueueView(x).sortOrder = row("sortOrder")
                        x += 1
                    Else
                        If Array.Exists(arrQueueView, row("labelName")) Then
                        Else
                            ReDim Preserve arrQueueView(x)
                            arrQueueView(x).groupID = row("groupID")
                            arrQueueView(x).labelName = row("labelName")
                            arrQueueView(x).sortOrder = row("sortOrder")
                            x += 1
                        End If
                    End If
                Next
    When the highlighted code is executed....I get an "Object reference not set to an instance of an object" error.
    Blake

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Using Array.Exists?

    Well, I would guess the reason is that row("labelName") returns Nothing. However, the second parameter in the Exists call should be of type Predicate.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Using Array.Exists?

    what is the Predicate? I've seen this term before but I don't understand how it's used.
    Blake

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Using Array.Exists?

    A Predicate is basically a delegate, ie it wraps a method-pointer. Here's how it would be applied in the Array.Exists case:
    (This example checks wether the value 4 exists within an integer array)
    VB.NET Code:
    1. Private Sub someSubroutine()
    2.         Dim myIntArray() As Integer = {1, 2, 3, 4}
    3.         Dim valueExists As Boolean = Array.Exists(myIntArray, New Predicate(Of Integer)(AddressOf myCompareFunction))
    4.     End Sub
    5.  
    6.     Private Function myCompareFunction(ByVal value As Integer) As Boolean
    7.         Return (value = 4)
    8.     End Function
    So basically you create a function that takes one argument which must be the same type as the array you're passing to the Exists function. It must also have the return type Boolean.
    The Exists method will iterate through each element in your array and pass them to this function, as soon as any element returns True, Exists will return true.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Using Array.Exists?

    when i searched for 'array.exists' this was fourth from the top

    http://msdn.microsoft.com/en-us/libr...be(VS.85).aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Using Array.Exists?

    This totally blows my mind only because the value to be searched seems as though it's hard-coded, which obviously can't be. Does anyone have a real example of this. That might help me if I can see it in use realtime.

    Thanks,
    Blake

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Using Array.Exists?

    Code:
        Dim lookfor As String = "fubar"
    
        Dim dinosaurs() As String = {"Compsognathus", _
            "Amargasaurus", "Oviraptor", "Velociraptor", _
            "Deinonychus", "Dilophosaurus", "Gallimimus", _
            "Triceratops"}
        Public Sub Main()
            Stop 'single step from here and see what happens
            dinosaurs(3) = lookfor
            If Array.Exists(dinosaurs, AddressOf EndsWithSaurus) Then
                Debug.WriteLine("found")
            End If
        End Sub
        Dim findThis As String = "fubar"
        Private Function EndsWithSaurus(ByVal s As String) _
                As Boolean
            If findThis = s Then
                Debug.WriteLine("Yes " & s)
                Return True
            Else
                Debug.WriteLine("No " & s)
                Return False
            End If
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width