I have the following setup:
Class A
property x as string
property y as int
property z as String
End Class
Class CollOfA
inherits List(Of A)
End Class
What I would like is an Item property in the collection that I can say:
dim c as new CollOfA
c.item("this", 2, "that")
I have tried implementing the following in CollOfA:
Class CollOfA
inherits List(Of A)
Default Public Overridable Shadows ReadOnly Property Item(ByVal x As String, ByVal y As Integer, byval z as string)
Get
' I want to do something like:
' ForEach item in me see if anything matches these three things
End Get
End Property
End Class
I know about predicates, but I am struggling with how to set the criteria of the predicate (i.e. passing x, y, and z).
Has anyone implemented something similar?
Here’s two ways that I came up with to accomplish your question. One way uses a LINQ query syntax to filter; the second uses a custom object to hold your predicate parameters and then uses that object to perform the filter.
Using the LINQ syntax in your Item property:
The other way would be to create a PredicateParameter class to hold your parameters and also a delegated method that is used to execute the filter. I saw this on an MSDN comment – here’s the link. Here’s the class:
And here’s the property that is in the CollOfA class that uses it:
Lastly, here’s a console runner to test this.
Hopefully this helps. There may be a more elegant way to do this, but I didn’t see one. (BTW, I generally work with C#, so my VB.NET is a little rusty.)