How to use Find with <T> of objects

G

Guest

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 to a FOR Loop.

public List<string> dinosaurs = new List<string>();
....
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
....
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); // iterates the
list// repeatedly calling function
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}

BUT ........
I have 2 issues, (1) I need to convey to the FIND what needs to be compared
dynamically, and (2) I need a way for the find to use it..- So far it wont
compile.

Suggestions please and THANK YOU in advance..

private List<FileRevisonInfo> lstReleaseFiles= new List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfo> sublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldName);
// WONT COMPILE Error 2 Cannot implicitly convert type
'MasterRelease2.FileRevisonInfo' to 'System.Collections.Generic.List
//<MasterRelease2.FileRevisonInfo>'

private static bool FindFieldName(FileRevisonInfo s)
{//must it read a property for theFieldName to be dynamic ??
string theFieldName = ""; // placeholder until method understood. ??
if (s.FName.ToUpper() == theFieldName)
{ return true; }
else
{ return false;}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");

// Find all the things that end with:
string endsWith = "saurus";

// Add an anonymous delegate which will use the endsWith value to find the
item.
List<string> sublist = dinosaurs.FindAll(
delegate(string obj)
{
// Return true if the string ends with the vale of endsWith.
return obj.EndsWith(endsWith);
};

Hope this helps.
 
G

Guest

I could not quite post the code in without showing red issues, - so I am not
sure what is not right, but I understand that anonymouse delegates are a good
place to start and I found
http://msdn.microsoft.com/msdnmag/issues/04/05/C20/ regarding this. I will
try and figure it out for awhile, thanks !
--
Andrew


Nicholas Paldino said:
Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");

// Find all the things that end with:
string endsWith = "saurus";

// Add an anonymous delegate which will use the endsWith value to find the
item.
List<string> sublist = dinosaurs.FindAll(
delegate(string obj)
{
// Return true if the string ends with the vale of endsWith.
return obj.EndsWith(endsWith);
};

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



andrewcw said:
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 to a FOR Loop.

public List<string> dinosaurs = new List<string>();
...
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); // iterates the
list// repeatedly calling function
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}

BUT ........
I have 2 issues, (1) I need to convey to the FIND what needs to be
compared
dynamically, and (2) I need a way for the find to use it..- So far it wont
compile.

Suggestions please and THANK YOU in advance..

private List<FileRevisonInfo> lstReleaseFiles= new
List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfo> sublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldName);
// WONT COMPILE Error 2 Cannot implicitly convert type
'MasterRelease2.FileRevisonInfo' to 'System.Collections.Generic.List
//<MasterRelease2.FileRevisonInfo>'

private static bool FindFieldName(FileRevisonInfo s)
{//must it read a property for theFieldName to be dynamic ??
string theFieldName = ""; // placeholder until method understood. ??
if (s.FName.ToUpper() == theFieldName)
{ return true; }
else
{ return false;}
}
 
G

Guest

OK the end was missing a ")" as in
});

Very slick. That was part 1 of my question. The second part dealt with types
other than strings... Thanks
--
Andrew


Nicholas Paldino said:
Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");

// Find all the things that end with:
string endsWith = "saurus";

// Add an anonymous delegate which will use the endsWith value to find the
item.
List<string> sublist = dinosaurs.FindAll(
delegate(string obj)
{
// Return true if the string ends with the vale of endsWith.
return obj.EndsWith(endsWith);
};

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



andrewcw said:
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 to a FOR Loop.

public List<string> dinosaurs = new List<string>();
...
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); // iterates the
list// repeatedly calling function
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}

BUT ........
I have 2 issues, (1) I need to convey to the FIND what needs to be
compared
dynamically, and (2) I need a way for the find to use it..- So far it wont
compile.

Suggestions please and THANK YOU in advance..

private List<FileRevisonInfo> lstReleaseFiles= new
List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfo> sublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldName);
// WONT COMPILE Error 2 Cannot implicitly convert type
'MasterRelease2.FileRevisonInfo' to 'System.Collections.Generic.List
//<MasterRelease2.FileRevisonInfo>'

private static bool FindFieldName(FileRevisonInfo s)
{//must it read a property for theFieldName to be dynamic ??
string theFieldName = ""; // placeholder until method understood. ??
if (s.FName.ToUpper() == theFieldName)
{ return true; }
else
{ return false;}
}
 

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

Top