473,395 Members | 1,692 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,395 software developers and data experts.

How to use Find with <T> of objects

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<stringdinosaurs = new List<string>();
....
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
....
List<stringsublist = 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<FileRevisonInfolstReleaseFiles= new List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfosublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldNam e);
// 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;}
}
--
Andrew
Aug 7 '06 #1
3 5030
Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<stringdinosaurs = 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<stringsublist = 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]
- mv*@spam.guard.caspershouse.com

"andrewcw" <an******@acw.comwrote in message
news:D6**********************************@microsof t.com...
>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<stringdinosaurs = new List<string>();
...
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<stringsublist = 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<FileRevisonInfolstReleaseFiles= new
List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfosublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldNam e);
// 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;}
}
--
Andrew

Aug 7 '06 #2
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 [.NET/C# MVP]" wrote:
Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<stringdinosaurs = 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<stringsublist = 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]
- mv*@spam.guard.caspershouse.com

"andrewcw" <an******@acw.comwrote in message
news:D6**********************************@microsof t.com...
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<stringdinosaurs = new List<string>();
...
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<stringsublist = 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<FileRevisonInfolstReleaseFiles= new
List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfosublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldNam e);
// 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;}
}
--
Andrew


Aug 7 '06 #3
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 [.NET/C# MVP]" wrote:
Andrew,

This is a perfect spot to use anonymous delegates.

You can do this:

public List<stringdinosaurs = 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<stringsublist = 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]
- mv*@spam.guard.caspershouse.com

"andrewcw" <an******@acw.comwrote in message
news:D6**********************************@microsof t.com...
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<stringdinosaurs = new List<string>();
...
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<stringsublist = 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<FileRevisonInfolstReleaseFiles= new
List<FileRevisonInfo>();
// fill my objects, add to list...

List<FileRevisonInfosublist =
(FileRevisonInfo)lstReleaseFiles.Find(FindFieldNam e);
// 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;}
}
--
Andrew


Aug 7 '06 #4

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

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
4
by: Samuel | last post by:
Hi, Say you have the following XML: <item ref="1"> <name>item 1</name> </item> <item ref="2"> <name>item 2</name> </item>
8
by: michael | last post by:
Hi all A client of mine is having a problem with their site and when I looked into the SQL database, I found that most text fields have been altered and appended with script...
2
by: David Thielen | last post by:
Hi; I have a small XML file that I need to read/change from my app. Is there some easy way to map from XML to my objects so I can just read it in to my objects, change the objects as needed,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.