I have a custom class that inherits from List(of T). How can I find an object from the List with a custom property?
Sample code:
Public class baseClass
Property ID
End class
Public class Member
Inherits BaseClass
Property UserID As Integer
End Class
Public Class LCMSCollection(Of T As BaseClass)
Inherits System.Collections.Generic.List(Of T)
End Class
What I want to be able to do is find a member by UserId. I would prefer something like this:
Dim userId = 3
Dim members = MemberService.GetMembers(<some parameter>)
Dim member = members.<some method name>(userId)
In other circumstances it would be:
Dim playerid = 6
Dim team = TeamService.GetTeam(<some parameter>)
Dim player = teams.<some method name>(playerid)
I'm thinking about a delegate and changing the Property to a function, but I don't know how to proceed.
Currently I have this:
Dim playerid = 6
Dim team = TeamService.GetTeam(<some parameter>)
Dim player = TeamService.GetPlayer(team, playerid)
This is not satisfying for several reasons. First of all getting an item from a collection should be done on the collection like getting an item from a list. Futhermore in the current situation one needs to add custom methods for each an every instance type of the collection. To avoid that one could create a general method getItem in a base class ad have all repositories like in this example Teams and Members inherit from this base class. BUt this cannot account for the fact that every instance class of baseclass has a different property or method to use as a filter. Doing it like this seems to me to introduce interdependencies and complexity which I was trying to avoid by using a generic collection.
Any suggestions? Am I trying to solve a non issue or approaching this from the wrong angle.
Btw I use both C# and VBnet so answers in either one language is OK