The topics described here concentrates fully on pure .Net Framework, describing actual meaning of some programming concepts, FCL and best practices. However you will be using these concepts in all . Net framework compatible languages like Microsoft visual basic .net (VB.Net) or C# .Net (csharp.Net) to build a web application (Asp.Net) or Desktop applications (winforms .net) or Web/Windows services





This blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://Codemine.net
and update your bookmarks.

Tuesday, November 6, 2007

Code for List.Find in . Net Framework 2.0

In one of my project I was in need of the List.FindAll method in . Net 2.0. I was unable to find any useful enough details about this, even though I did a good amount of searching in google. After a while, I ended up in the following code . My need was to find the exact object of my custom class in the collection given, matching my criteria. The List.FindAll accepts a predicate as parameter in which we can specify our own criteria. Predicate (System.predicate) is a new facility introduced in .Net 2.0 which enables us to mention our custom criteria.

Here is the code:
public class MYList:List<MyCustomCLASS>
{
private int currentindex;
///


/// Constructor
///

public MYList ()
{}


public List<> GetItemsContaining(double text)
{
return this.FindAll(delegate(MyCustomCLASS myobj) {return myobj.ItemId == text; });

}

Here the GetItemsContaining uses the base class‘s FindAll method to check and return a list of objects matching the criteria. We can also use List.Find (if you want a single object to be returned from the colllection) and List.ConvertAll (Which converts all objects in the collection to specified objects) in the same logic.

And the MyCustomCLASS is nothing but a normal class which contains an ItemId as one of it's property and ofcourse... ya it contaims some other properties too which holds the buisness specific data records

No comments: