[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.
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.
Re: [2005] Using Array.Exists?
what is the Predicate? I've seen this term before but I don't understand how it's used.
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:
Private Sub someSubroutine()
Dim myIntArray() As Integer = {1, 2, 3, 4}
Dim valueExists As Boolean = Array.Exists(myIntArray, New Predicate(Of Integer)(AddressOf myCompareFunction))
End Sub
Private Function myCompareFunction(ByVal value As Integer) As Boolean
Return (value = 4)
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.
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
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,
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