Tuesday, 19 July 2011

How to delegate using ASP.NET 2.0 and C#.NET


This tutorial demonstrates how to sort employee of one company by the employee's salary asc with the Delegate technique and bubble sort.

This tutorial demonstrates how to sort employee of one company by the employee's salary asc with the Delegate technique and bubble sort. And this tutorial only using the default namespace.
A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.

First, we need to  Declares a delegate. And we use the btnSorter_Click event to do the work.

public delegate bool CompareOB(object lhs,object rhs);

protected void btnSorter_Click(object sender, EventArgs e)
{
string employeelist = "";
Employee[] employees =
{
new Employee(this.lbljoy.Text.Trim(),Convert.ToDecimal(this.lblsalaryjoy.Text.Trim())),
new Employee(this.lbljake.Text.Trim(),Convert.ToDecimal(this.lblsalaryjake.Text.Trim())),
new Employee(this.lblpetter.Text.Trim(),Convert.ToDecimal(this.lblsalarypetter.Text.Trim())),
new Employee(this.lblsam.Text.Trim(),Convert.ToDecimal(this.lblsalarysam.Text.Trim()))
};
CompareOB employeeCompareOB = new CompareOB(Employee.isGreater);
BubbleSorter.Sort(employees, employeeCompareOB);
for(int i=0;i<employees.Length;i++)
employeelist += employees[i].ToString() + "<br>";

this.lblResult.Text = employeelist.ToString();

}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

The code of the Employee class as follows

public class Employee
{
private string name;
private decimal salary;

public Employee(string name,decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string ToString()
{
return string.Format(name +":{0:C}",salary);
}
public static bool isGreater(object lhs, object rhs)
{
Employee employeelhs = (Employee)lhs;
Employee employeerhs = (Employee)rhs;
return (employeerhs.salary > employeelhs.salary) ? true : false;
}
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The code of the BubbleSorter class as follows

public class BubbleSorter
{
public static void Sort(object[] sortArry, CompareOB gtMethod)
{
for (int i = 0; i < sortArry.Length; i++)
{
for (int j = i + 1; j < sortArry.Length; j++)
{
if(gtMethod(sortArry[j],sortArry[i]))
{
object temp = sortArry[i];
sortArry[i] = sortArry[j];
sortArry[j] = temp;
}
}
}
}
}

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The front end BubbleSorterWithDelegateVB.aspx page looks something like this:

<table>
<tr>
<td style="width: 100px">name</td>
<td style="width: 100px">salary</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lbljoy" runat="server" Text="joy"></asp:Label></td>
<td style="width: 100px">
<asp:Label ID="lblsalaryjoy" runat="server" Text="20000"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lbljake" runat="server" Text="jake"></asp:Label></td>
<td style="width: 100px">
<asp:Label ID="lblsalaryjake" runat="server" Text="10000"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px; height: 21px;">
<asp:Label ID="lblpetter" runat="server" Text="petter"></asp:Label></td>
<td style="width: 100px; height: 21px;">
<asp:Label ID="lblsalarypetter" runat="server" Text="25000"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblsam" runat="server" Text="sam"></asp:Label></td>
<td style="width: 100px">
<asp:Label ID="lblsalarysam" runat="server" Text="23000"></asp:Label></td>
</tr>
</table>
&nbsp;
<asp:Button ID="btnSorter" runat="server" Text="Sort" Width="78px" OnClick="btnSorter_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Display the sorted result:"></asp:Label>&nbsp;&nbsp;<br />
<asp:Label ID="lblResult" runat="server" ForeColor="Red"></asp:Label>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace BubbleSorter
{
public delegate bool CompareOB(object lhs,object rhs);

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSorter_Click(object sender, EventArgs e)
{
string employeelist = "";
Employee[] employees =
{
new Employee(this.lbljoy.Text.Trim(),Convert.ToDecimal(this.lblsalaryjoy.Text.Trim())),
new Employee(this.lbljake.Text.Trim(),Convert.ToDecimal(this.lblsalaryjake.Text.Trim())),
new Employee(this.lblpetter.Text.Trim(),Convert.ToDecimal(this.lblsalarypetter.Text.Trim())),
new Employee(this.lblsam.Text.Trim(),Convert.ToDecimal(this.lblsalarysam.Text.Trim()))
};
CompareOB employeeCompareOB = new CompareOB(Employee.isGreater);
BubbleSorter.Sort(employees, employeeCompareOB);
for(int i=0;i<employees.Length;i++)
employeelist += employees[i].ToString() + "<br>";

this.lblResult.Text = employeelist.ToString();
}
}

public class Employee
{
private string name;
private decimal salary;

public Employee(string name,decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string ToString()
{
return string.Format(name +":{0:C}",salary);
}
public static bool isGreater(object lhs, object rhs)
{
Employee employeelhs = (Employee)lhs;
Employee employeerhs = (Employee)rhs;
return (employeerhs.salary > employeelhs.salary) ? true : false;
}
}

public class BubbleSorter
{
public static void Sort(object[] sortArry, CompareOB gtMethod)
{
for (int i = 0; i < sortArry.Length; i++)
{
for (int j = i + 1; j < sortArry.Length; j++)
{
if(gtMethod(sortArry[j],sortArry[i]))
{
object temp = sortArry[i];
sortArry[i] = sortArry[j];
sortArry[j] = temp;
}
}
}
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment