I've read through pages of posts on using the FIND() method in the forums, but for some reason I'm still having problems. I'm new to .NET and quite confused on the syntax/declaration of the predicate delegate stuff. I have read the links on msdn, read numerous posts here and I'm not sure why I'm not quite grasping what I need to, so hopefully someone out there can help. Here is what I'm trying to do.
--- Create a very simple object ---
Public Class TestObject
Private theId As Integer
Public Property intId() As Integer
Get
Return theId
End Get
Set(ByVal passedInId As Integer)
theId = passedInId
End Set
End Property
End Class
-- Create a class which populates the List and retrieves from the List ----
Public Class PopulateList
Public Function PopulateTheList() As TestObject
Dim TestObjectList As List(Of TestObject)
'Create some dummy objects and populate the list
Dim TestObject1 As TestObject
TestObject1.intId = 1
TestObjectList.add(TestObject1)
Dim TestObject2 As TestObject
TestObject2.intId = 2
TestObjectList.add(TestObject2)
Dim TestObject3 As TestObject
TestObject3.intId = 3
TestObjectList.add(TestObject3)
'Find the test object where the id = 2
Dim TestObjectFound As TestObject
TestObjectList.Find(??)
Return TestObjectFound
End Function
End Class
------------
Now I am confused when it comes to the predicate. Here are my points of confusion:
1) Can I create a third class to hold the function? Better yet, where would this function actually exist...in my TestObject or PopulateList class?
2) As much as I've read on it, I'm still unclear on the proper syntax to create it. Assuming I can create it in a third class, I expect it to be something like this:
Public Class TestFilter
Public Function IdEquals2(ByVal PassedInObject As TestObject) As Boolean
If PassedInObject.intId = 2 Then
Return True
Else
Return False
End If
End Function
End Class
3) The syntax for the call to the Find() above....
TestObjectList.Find(??)
Once again, I am new to .NET and searched a good deal for this, I just wasn't able to track down my answer. I'm sure it is out there and I may have read it, but for some reason I just didn't quite catch on. Hopefully someone out there can help me out.
Thanks!
I seem to have a solution. I created a wrapper class for the predicate which included a delegate declaration, declared my filtering function as shared and everything seems to be functioning. Incase anyone was taking some time looking at this, many thanks. If anyone is looking for a similar solution and couldn't find one, let me know and I can send around what I have. I'm very new to .NET, so I'm not sure it is the smoothest way to do it, but it does work and saves me having to loop through the entire List.
0 comments:
Post a Comment