473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 VersionPlanAttr ibutes As New _
System.Collecti ons.Generic.Lis t(Of Attribute)
Public Traces As New System.Collecti ons.Generic.Lis t(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.Collecti ons.Generic.Lis t(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 8267
Joerg,

"Joerg Battermann" <jb@justbe.co m> schrieb:
I have a custom type defined via

[...]
Public Class Requirement
Public IDNumber As Integer
Public Name As String
Public Description As String
Public VersionPlanAttr ibutes As New _
System.Collecti ons.Generic.Lis t(Of Attribute)
Public Traces As New System.Collecti ons.Generic.Lis t(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.Collecti ons.Generic.Lis t(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)(Address Of 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)(Address Of 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.co m> schrieb:
I have a custom type defined via

[...]
Public Class Requirement
Public IDNumber As Integer
Public Name As String
Public Description As String
Public VersionPlanAttr ibutes As New _
System.Collecti ons.Generic.Lis t(Of Attribute)
Public Traces As New System.Collecti ons.Generic.Lis t(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.Collecti ons.Generic.Lis t(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)(Address Of 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.co m> schrieb:
thanks for the quick reply, however is it possible to define a name in
your example using the Predicate(Of Person)(Address Of 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(By Val obj As Person) As
Boolean
Return obj.Name = DirectCast(Para meters(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(Addre ssOf 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.co m> schrieb:
thanks for the quick reply, however is it possible to define a name in
your example using the Predicate(Of Person)(Address Of 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(By Val obj As Person) As
Boolean
Return obj.Name = DirectCast(Para meters(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(Addre ssOf 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.co m> wrote in message
news:43******** *************** @newsread2.arco r-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 VersionPlanAttr ibutes As New _
| System.Collecti ons.Generic.Lis t(Of Attribute)
| Public Traces As New System.Collecti ons.Generic.Lis t(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.Collecti ons.Generic.Lis t(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
10293
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 get a servlet error "cannot find collection". Not usre if my issue is HTML, JSP, WebSphere or ??? Any help is very much appreciated. HTML file: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
5
2967
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
4061
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). Is there a way of using a custom indexer (for example Sensors) that does *not* need to loop through each item in the collection and compare them?
2
3548
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 containing: { 1.102, "fox" } { 1.592, "cat" } { 2.239, "dog" } how do you get an interator that points to the location where { 1.910, "otter" } would be inserted? (I guess in this case the functional
2
1828
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 loop (for int i=...). Reason: I need to fill up an updatable dataset with all fields entered on the asp.net page instead of manually going 1 field at a time and hard code the control name (i.e. TextBoxFirstName.Text=something and so on) ...
6
8892
by: TomislaW | last post by:
How to find all user controls (ascx) loaded on a Page?
9
5044
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: ******************************************************************************* <asp:DataGrid visible="False" border=1
2
1529
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 ? I need to know which controls changed during TestEvent. I can not compare them before and after TestEvent, because it's not possible to implement compare for all kind of possible controls that Page object may contain.
22
4704
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: collection keys are 20, 30, 40, 50, 60, 70, 80 find key which is >= 35. would return the 30 key.
0
7947
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8010
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6665
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.