Find in Collection

  • Thread starter Thread starter Joerg Battermann
  • Start date Start date
J

Joerg Battermann

Hello there,

I have a custom type defined via

[...]
Public Class Requirement
Public IDNumber As Integer
Public Name As String
Public Description As String
Public VersionPlanAttributes As New _
System.Collections.Generic.List(Of Attribute)
Public Traces As New System.Collections.Generic.List(Of Trace)
Public Checked As Boolean = False
End Class
[...]

and want to find one particular item (identified via IDNumber) in a
filled collection of these items declared via

[...]
Public ERSRequirements As New System.Collections.Generic.List(Of_
Requirement)
[...]


... is there any quick way to do it or would I have to loop through the
collection?


Best Regards and thanks,
-Joerg
 
Joerg,

Joerg Battermann said:
I have a custom type defined via

[...]
Public Class Requirement
Public IDNumber As Integer
Public Name As String
Public Description As String
Public VersionPlanAttributes As New _
System.Collections.Generic.List(Of Attribute)
Public Traces As New System.Collections.Generic.List(Of Trace)
Public Checked As Boolean = False
End Class
[...]

and want to find one particular item (identified via IDNumber) in a filled
collection of these items declared via

[...]
Public ERSRequirements As New System.Collections.Generic.List(Of_
Requirement)
[...]


.. is there any quick way to do it or would I have to loop through the
collection?

\\\
Public Class Form1
Private Sub Test()
Dim x As New List(Of Person)
x.Add(New Person("Peter", 1099))
x.Add(New Person("Frank", 342234))
x.Add(New Person("Fergus", 23423))
x.Add(New Person("Bill", 234))
Dim p As Person = _
x.Find(New Predicate(Of Person)(AddressOf GetFrank))
MsgBox(p.Name)
End Sub

Private Function GetFrank( _
ByVal obj As Person _
) As Boolean
Return obj.Name = "Frank"
End Function
End Class

Public Class Person
Public Sub New(ByVal Name As String, ByVal Salary As Double)
Me.Name = Name
Me.Salary = Salary
End Sub

Public Name As String
Public Salary As Double
End Class
///

Collections are unsorted, thus runtime is in O(n) if the collection contains
n items.
 
Hey there,

thanks for the quick reply, however is it possible to define a name in
your example using the Predicate(Of Person)(AddressOf GetFrank)?

I am calling a function with an IDNumber as variable and it shall find
me the one Requirement that has that particular IDNumber... and return
it or do something with it


does this make sense?

-j
Joerg,

Joerg Battermann said:
I have a custom type defined via

[...]
Public Class Requirement
Public IDNumber As Integer
Public Name As String
Public Description As String
Public VersionPlanAttributes As New _
System.Collections.Generic.List(Of Attribute)
Public Traces As New System.Collections.Generic.List(Of Trace)
Public Checked As Boolean = False
End Class
[...]

and want to find one particular item (identified via IDNumber) in a
filled collection of these items declared via

[...]
Public ERSRequirements As New System.Collections.Generic.List(Of_
Requirement)
[...]


.. is there any quick way to do it or would I have to loop through the
collection?

\\\
Public Class Form1
Private Sub Test()
Dim x As New List(Of Person)
x.Add(New Person("Peter", 1099))
x.Add(New Person("Frank", 342234))
x.Add(New Person("Fergus", 23423))
x.Add(New Person("Bill", 234))
Dim p As Person = _
x.Find(New Predicate(Of Person)(AddressOf GetFrank))
MsgBox(p.Name)
End Sub

Private Function GetFrank( _
ByVal obj As Person _
) As Boolean
Return obj.Name = "Frank"
End Function
End Class

Public Class Person
Public Sub New(ByVal Name As String, ByVal Salary As Double)
Me.Name = Name
Me.Salary = Salary
End Sub

Public Name As String
Public Salary As Double
End Class
///

Collections are unsorted, thus runtime is in O(n) if the collection
contains n items.
 
Joerg Battermann said:
thanks for the quick reply, however is it possible to define a name in
your example using the Predicate(Of Person)(AddressOf GetFrank)?

I am calling a function with an IDNumber as variable and it shall find me
the one Requirement that has that particular IDNumber... and return it or
do something with it

I didn't have enough time to play around with this, but maybe the solution
below will help you solve the problem (quick and dirty!):

\\\
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x As New List(Of Person)
x.Add(New Person("Peter", 1099))
x.Add(New Person("Frank", 342234))
x.Add(New Person("Fergus", 23423))
x.Add(New Person("Bill", 234))
Dim p As Person = (New PersonFinder()).Find(x, "Frank")
MsgBox(p.Name)
End Sub
End Class

Public Class PersonFinder
Inherits Finder(Of Person)

Protected Overrides Function InternalFind(ByVal obj As Person) As
Boolean
Return obj.Name = DirectCast(Parameters(0), String)
End Function
End Class

Public Class Person
Public Sub New(ByVal Name As String, ByVal Salary As Double)
Me.Name = Name
Me.Salary = Salary
End Sub

Public Name As String
Public Salary As Double
End Class

Public MustInherit Class Finder(Of T)
Private m_Parameters() As Object

Protected Property Parameters() As Object()
Get
Return m_Parameters
End Get
Private Set(ByVal Value As Object())
m_Parameters = Value
End Set
End Property

Public Function Find( _
ByVal List As List(Of T), _
ByVal ParamArray Parameters() As Object _
) As T
Me.Parameters = Parameters
Return List.Find(AddressOf InternalFind)
End Function

Protected Overridable Function InternalFind( _
ByVal obj As T _
) As Boolean
'
End Function
End Class
///
 
Thanks a lot!!

I didn't have enough time to play around with this, but maybe the
solution below will help you solve the problem (quick and dirty!):

\\\
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x As New List(Of Person)
x.Add(New Person("Peter", 1099))
x.Add(New Person("Frank", 342234))
x.Add(New Person("Fergus", 23423))
x.Add(New Person("Bill", 234))
Dim p As Person = (New PersonFinder()).Find(x, "Frank")
MsgBox(p.Name)
End Sub
End Class

Public Class PersonFinder
Inherits Finder(Of Person)

Protected Overrides Function InternalFind(ByVal obj As Person) As
Boolean
Return obj.Name = DirectCast(Parameters(0), String)
End Function
End Class

Public Class Person
Public Sub New(ByVal Name As String, ByVal Salary As Double)
Me.Name = Name
Me.Salary = Salary
End Sub

Public Name As String
Public Salary As Double
End Class

Public MustInherit Class Finder(Of T)
Private m_Parameters() As Object

Protected Property Parameters() As Object()
Get
Return m_Parameters
End Get
Private Set(ByVal Value As Object())
m_Parameters = Value
End Set
End Property

Public Function Find( _
ByVal List As List(Of T), _
ByVal ParamArray Parameters() As Object _
) As T
Me.Parameters = Parameters
Return List.Find(AddressOf InternalFind)
End Function

Protected Overridable Function InternalFind( _
ByVal obj As T _
) As Boolean
'
End Function
End Class
///
 
Joerg,
In addition to the other comments.

I posted a sample Generic Find routine in April that uses Predicate(Of T, V)
instead of Predicate(Of T). Predicate(Of T, V) allows you to pass the value
you are looking for in addition to the object you are looking at.

See these posts:

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/98f83b1dab29e1c8?hl=en&

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/649720602d7da920?hl=en&

Reviewing the entire thread might be helpful:
http://groups.google.com/group/micr...88a682c0252de224?tvc=1&hl=en#88a682c0252de224


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello there,
|
| I have a custom type defined via
|
| [...]
| Public Class Requirement
| Public IDNumber As Integer
| Public Name As String
| Public Description As String
| Public VersionPlanAttributes As New _
| System.Collections.Generic.List(Of Attribute)
| Public Traces As New System.Collections.Generic.List(Of Trace)
| Public Checked As Boolean = False
| End Class
| [...]
|
| and want to find one particular item (identified via IDNumber) in a
| filled collection of these items declared via
|
| [...]
| Public ERSRequirements As New System.Collections.Generic.List(Of_
| Requirement)
| [...]
|
|
| .. is there any quick way to do it or would I have to loop through the
| collection?
|
|
| Best Regards and thanks,
| -Joerg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top