Tuesday, 19 July 2011

How to delegate using ASP.NET 2.0 and VB.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 Function CompareOB(ByVal lhs As Object, ByVal rhs As Object) As Boolean

Protected Sub btnSorter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSorter.Click
Dim employeelist As String = ""
Dim employees As Employee() = {New Employee(Me.lbljoy.Text.Trim(), Convert.ToDecimal(Me.lblsalaryjoy.Text.Trim())), New Employee(Me.lbljake.Text.Trim(), Convert.ToDecimal(Me.lblsalaryjake.Text.Trim())), New Employee(Me.lblpetter.Text.Trim(), Convert.ToDecimal(Me.lblsalarypetter.Text.Trim())), New Employee(Me.lblsam.Text.Trim(), Convert.ToDecimal(Me.lblsalarysam.Text.Trim()))}
Dim employeeCompareOB As New CompareOB(AddressOf Employee.isGreater)
BubbleSorter.Sort(employees, employeeCompareOB)
Dim i As Integer

For i = 0 To employees.Length - 1
employeelist += employees(i).ToString() + "<br>"
Next i

Me.lblResult.Text = employeelist.ToString()
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The code of the Employee class as follows

Public Class Employee
Private name As String
Private salary As Decimal

Public Sub New(ByVal name As String, ByVal salary As Decimal)
Me.name = name
Me.salary = salary
End Sub

Public Overrides Function ToString() As String
Return String.Format(name + ":{0:C}", salary)
End Function

Public Shared Function isGreater(ByVal lhs As Object, ByVal rhs As Object) As Boolean
Dim employeelhs As Employee = CType(lhs, Employee)
Dim employeerhs As Employee = CType(rhs, Employee)
Return IIf(employeerhs.salary > employeelhs.salary, True, False)
End Function
End Class

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The code of the BubbleSorter class as follows

Public Class BubbleSorter
Public Shared Sub Sort(ByVal sortArry() As Object, ByVal gtMethod As CompareOB)
Dim i As Integer
For i = 0 To sortArry.Length - 1
Dim j As Integer
For j = i + 1 To sortArry.Length - 1
If gtMethod(sortArry(j), sortArry(i)) Then
Dim temp As Object = sortArry(i)
sortArry(i) = sortArry(j)
sortArry(j) = temp
End If
Next j
Next i
End Sub
End Class

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

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

The flow for the code behind page is as follows.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Namespace BubbleSorter
Public Delegate Function CompareOB(ByVal lhs As Object, ByVal rhs As Object) As Boolean

Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Protected Sub btnSorter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSorter.Click
Dim employeelist As String = ""
Dim employees As Employee() = {New Employee(Me.lbljoy.Text.Trim(), Convert.ToDecimal(Me.lblsalaryjoy.Text.Trim())), New Employee(Me.lbljake.Text.Trim(), Convert.ToDecimal(Me.lblsalaryjake.Text.Trim())), New Employee(Me.lblpetter.Text.Trim(), Convert.ToDecimal(Me.lblsalarypetter.Text.Trim())), New Employee(Me.lblsam.Text.Trim(), Convert.ToDecimal(Me.lblsalarysam.Text.Trim()))}
Dim employeeCompareOB As New CompareOB(AddressOf Employee.isGreater)
BubbleSorter.Sort(employees, employeeCompareOB)
Dim i As Integer

For i = 0 To employees.Length - 1
employeelist += employees(i).ToString() + "<br>"
Next i
Me.lblResult.Text = employeelist.ToString()
End Sub
End Class

Public Class Employee
Private name As String
Private salary As Decimal

Public Sub New(ByVal name As String, ByVal salary As Decimal)
Me.name = name
Me.salary = salary
End Sub

Public Overrides Function ToString() As String
Return String.Format(name + ":{0:C}", salary)
End Function

Public Shared Function isGreater(ByVal lhs As Object, ByVal rhs As Object) As Boolean
Dim employeelhs As Employee = CType(lhs, Employee)
Dim employeerhs As Employee = CType(rhs, Employee)
Return IIf(employeerhs.salary > employeelhs.salary, True, False)
End Function
End Class

Public Class BubbleSorter
Public Shared Sub Sort(ByVal sortArry() As Object, ByVal gtMethod As CompareOB)
Dim i As Integer
For i = 0 To sortArry.Length - 1
Dim j As Integer
For j = i + 1 To sortArry.Length - 1
If gtMethod(sortArry(j), sortArry(i)) Then
Dim temp As Object = sortArry(i)
sortArry(i) = sortArry(j)
sortArry(j) = temp
End If
Next j
Next i
End Sub
End Class
End Namespace

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment