Search

Showing posts with label Delegates. Show all posts
Showing posts with label Delegates. Show all posts

Dec 8, 2008

Querying Selected Items From ListItemCollection Using LINQ

Working with ListItemCollection is general practices and getting single value for that it's like child's play. What if you need the list of items which are selected?

I am sure you probably use foreach loop something like

List<ListItem> selectedList = new List<ListItem>();
foreach(ListItem li in cbList.Items)
{
if (li.Selected)
selectedList.Add(li);
}

Or advance user use the functionality of C#2.0 using yield keyword

private IEnumerable<ListItem> GetSelectedItems(ListControl listControl)
{
foreach (ListItem itm in listControl.Items)
{
if (itm.Selected)
yield return itm;
}
}
.
.
.
.
.
.
{
IEnumerable<ListItem> selectedList = new List<ListItem>();
selectedList = GetSelectedItems(cbList);
}
And now using LINQ C#3.0

var selectedItems =
from li in cbList.Items.Cast<ListItem>()
where li.Selected == true
select li;

The only difference is the Cast, we need to make ListItemCollection generic so we can use it in LINQ query.

And now lets see using Lambda Expression C#3.0

List<ListItem> selectedItems =
cbList.Items.Cast<ListItem>()
.Where(item => item.Selected == true).ToList();

Here also we did cast to get the extension method Where. And in Where condition we write the lambda expression instead of anonymous delegate function.

We can even write extension method on base class ListControl so we can have selected items for all the list control. Lets write one extension method on ListControl

public static List<ListItem> SelectedItems(this ListControl lc)
{
List<ListItem> selectedItems =
lc.Items.Cast<ListItem>()
.Where(item => item.Selected == true).ToList();
return selectedItems;
}

And get the selected items using our extension method we will just call the SelectedItems method.

CheckBoxList cbList = new CheckBoxList();
cbList.SelectedItems();

Aug 12, 2008

How to sort generic collection

Hi all,

We will now sort generic collection. For this we need to create one comparere class which inherits IComparer<T> and implement Compare method, where T is your class object. Compare method is responsible to compare tow object and based on the result sorting will be done.

public class TestSortComparere : IComparer<TestSort>
{
public TestSortComparere() { }

#region IComparer<TestSort> Members

public int Compare(TestSort x, TestSort y)
{
return string.Compare(x.a, y.a);
}

#endregion
}

Now lets call Sort method.

List<TestSort> MyObjList = new List<TestSort>();
MyObjList.Add(new TestSort("Imran"));
MyObjList.Add(new TestSort("Dipak"));
MyObjList.Add(new TestSort("Sachin"));
MyObjList.Add(new TestSort("Darshan"));
MyObjList.Add(new TestSort("Gaurav"));

MyObjList.Sort(new TestSortComparere());

You can see the sorted order when you iterate MyObjList

foreach (TestSort testSort in MyObjList)
Console.Write(testSort.strTest + Environment.NewLine);
//OUTPUT
/*
Darshan
Dipak
Gaurav
Imran
Sachin
*/

We can change the sort line using Anonymous delegate. Lets see how, its only in single line!!!!.

MyObjList.Sort(delegate(TestSort x, TestSort y) { return string.Compare(x.strTest, y.strTest); });

Apr 29, 2008

Anonymous Delegate!!!

Here is one more powerful use of Delegate.

Till now I familiar with simple delegate and multicast delegate. Now one more and I found very good type of Delegate that is Anonymous Delegate.

In general Anonymous delegates are just a convenient way to declare a method without naming it.
Or
Passing a method to a method by typing in the method (including the curly braces), rather than the name of a method declared somewhere else.

A delegate is something like a function pointer, you can pass it to another method (as a parameter) and execute it remotely. Usually they are just a pointer to a method defined somewhere else in the class, but in the case of anonymous ones, they are defined in place. This makes it easier, because like this you don't need to look for a name


protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnLogin.Click += delegate { objClass.ValidateUser(); };
}
This is simple delegate with out any parameter. The click event of Login button will be handled by function ValidateUser of some class.

Now if you want any parameter in your delegate then before open curly braces you can add it just like simple function.

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnLogin.Click += delegate(object sender, EventArgs args) { presenter.ValidateUser(); };
}
As its function without name, you can also write your code there.

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptUserList.ItemCommand += delegate(object source, RepeaterCommandEventArgs ee)
{
Console.Write(int.Parse(ee.CommandArgument.ToString()));
objClass.GetDetailsById(int.Parse(ee.CommandArgument.ToString()));
//display the details
Console.Write(objClass.Name);

};
}
That is the basic idea.

Apr 28, 2008

Delegates and Events in C# / .NET

I come accross a good link for how Delegates and Events works in C#.

What are delegats, what is multicast-delegate handling events with delegates... all this with simple and understandable example.

Read more Delegates and Events in C# / .NET

Feb 15, 2007

Compare two objects!!!


Use IComparer Generic Interface.

This interface provides


public int Compare(object x, object y)

you overiddes this method and implement yours, so simple right?


Example:


public class MyComparer : IComparer
{
public MyComparer() { }

public int Compare(object x, object y)
{
return DateTime.Compare(((MyClass)x).EntryDate, ((MyClass)y).EntryDate);
}

}
A delicious related to this topic!