473,509 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find in Generic.List

Hi All

I'm trying to use a Generic.List declared as follows (Example):

Public Class MyObj
{
Public String Name;
Public Integer Age;
}
Public Class GenericTest
{

private System.Collections.Generic.List<MyObjTheList;
Public void Search()
{
string SearchForName = "Michael";
MyObj FoundObj;

FoundObj = TheList.Find(FindByName)
}

private bool FindByName(MyObj obj)
{
if (obj.Name == ???)
return true
else
return false;
}
}

I would like to find instances og MyObj by passing a Name value, but I can't
seem to find out how to make a Predicate that can do just that.

How do I pass the SearchForName to the Delegate/predicate ?????
Anyone has some code that will help me out here
THanks in advance

Allan Bredahl
Mar 16 '07 #1
3 14381
3 ways via C#; the 3rd is more-or-less what the compiler does with the
2nd - might be useful depending on how you can use inline delegates in
VB.Net (I honestly don't know):

public MyObj Search1(string name) {
foreach (MyObj obj in TheList) if (obj.Name == name) return
obj;
return null;
}
public MyObj Search2(string name) {
return TheList.Find(delegate(MyObj obj) { return obj.Name ==
name; });
}
public MyObj Search3(string name) {
return TheList.Find(new FindByName(name).IsMatch);
}
internal class FindByName {
private string name;
public FindByName(string name) {
this.name = name;
}
public bool IsMatch(MyObj obj) {
return obj.Name == name;
}
}
Mar 16 '07 #2

Bookmark this:
Using generics in C# 2.0
http://coders-lab.dnn-portal.com/Def...px?tabid=16942
That's a definate "go to" resource.

"Allan Bredahl" <al*@senseit.dkwrote in message
news:OR**************@TK2MSFTNGP05.phx.gbl...
Hi All

I'm trying to use a Generic.List declared as follows (Example):

Public Class MyObj
{
Public String Name;
Public Integer Age;
}
Public Class GenericTest
{

private System.Collections.Generic.List<MyObjTheList;
Public void Search()
{
string SearchForName = "Michael";
MyObj FoundObj;

FoundObj = TheList.Find(FindByName)
}

private bool FindByName(MyObj obj)
{
if (obj.Name == ???)
return true
else
return false;
}
}

I would like to find instances og MyObj by passing a Name value, but I
can't
seem to find out how to make a Predicate that can do just that.

How do I pass the SearchForName to the Delegate/predicate ?????
Anyone has some code that will help me out here
THanks in advance

Allan Bredahl


Mar 16 '07 #3
I had the same problem Allan.
I used this solution, by the way Find does not return bool it returns the
found object otherwise null.

Public Class MyObj
{
Public String Name;
Public Integer Age;
}
Public Class GenericTest
{

private System.Collections.Generic.List<MyObjTheList;
Public void Search()
{
string SearchForName = "Michael";
MyObj FoundObj;

FoundObj = TheList.Find(delegate(MyObj obj){return obj.Name ==
SearchForName ? obj : null ;})
}

}

"Allan Bredahl" wrote:
Hi All

I'm trying to use a Generic.List declared as follows (Example):

Public Class MyObj
{
Public String Name;
Public Integer Age;
}
Public Class GenericTest
{

private System.Collections.Generic.List<MyObjTheList;
Public void Search()
{
string SearchForName = "Michael";
MyObj FoundObj;

FoundObj = TheList.Find(FindByName)
}

private bool FindByName(MyObj obj)
{
if (obj.Name == ???)
return true
else
return false;
}
}

I would like to find instances og MyObj by passing a Name value, but I can't
seem to find out how to make a Predicate that can do just that.

How do I pass the SearchForName to the Delegate/predicate ?????
Anyone has some code that will help me out here
THanks in advance

Allan Bredahl
Apr 17 '07 #4

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

Similar topics

5
8262
by: Joerg Battermann | last post by:
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 _
8
1817
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
0
6830
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
3
5031
by: andrewcw | last post by:
I looked at this code on MSDN ( and at article in MSDN magazine this month ): The code below suggests that I may be able to use the find function of generics to locate the object easily as opposed...
0
5918
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
3
2247
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
3794
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
3276
by: Fresno Bob | last post by:
I have a generic collection of objects and I would like to find the object by one of it's properties e.g. I would like something with the functionality of something like...
3
2152
by: Tony Johansson | last post by:
Hello! Assume I have the class shown below and it's stored in a generic List. If I want to find the Value for a specific Id how do I use the find method. We can assume that the generic List...
0
7234
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
7136
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
7344
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,...
1
7069
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...
1
5060
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...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
775
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.