473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Find in Collection

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
Dec 1 '05 #1
5 8254
Joerg,

"Joerg Battermann" <jb@justbe.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 1 '05 #2
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

Herfried K. Wagner [MVP] wrote:
Joerg,

"Joerg Battermann" <jb@justbe.com> schrieb:
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.

Dec 1 '05 #3
"Joerg Battermann" <jb@justbe.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 1 '05 #4
Thanks a lot!!
Herfried K. Wagner [MVP] wrote:
"Joerg Battermann" <jb@justbe.com> schrieb:
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
///

Dec 1 '05 #5
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/micro...b29e1c8?hl=en&

http://groups.google.com/group/micro...d7da920?hl=en&

Reviewing the entire thread might be helpful:
http://groups.google.com/group/micro...a682c0252de224
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Joerg Battermann" <jb@justbe.com> wrote in message
news:43***********************@newsread2.arcor-online.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
Dec 2 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nc | last post by:
My iterator can find my collection when my Action class calls my jsp directly, however when my Action class calls an html file that is set up with IFrames (one of which is loading that same jsp), I...
5
by: Tom | last post by:
How can I find the first blank control on a form by looking at the controls in the order of their tab order? Thanks! Tom
0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
2
by: howard | last post by:
If you have a sorted collection, how do you find the location ^closest^ to where a particular key would be if an element with key isn't in the collection? For example, if you have a SortedArray...
2
by: TM | last post by:
What is the method used to find all asp.net server controls (run at server) such as TextBox, ListBox, CheckBox... currently defined on an Asp.Net page at run time. I need it inside of a controlled...
6
by: TomislaW | last post by:
How to find all user controls (ascx) loaded on a Page?
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
2
by: madani | last post by:
Hi, I made custom web control. this control have an Event like "TestEvent", How can I find out if properties of other controls of parent Page is changed inside this event when I fire that event...
22
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.