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 = "" End Sub 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 iMe.lblResult.Text = employeelist.ToString() |
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 End Class Private salary As Decimal Public Sub New(ByVal name As String, ByVal salary As Decimal) Me.name = name End SubMe.salary = salary Public Overrides Function ToString() As String Return String.Format(name + ":{0:C}", salary) End FunctionPublic Shared Function isGreater(ByVal lhs As Object, ByVal rhs As Object) As Boolean Dim employeelhs As Employee = CType(lhs, Employee) End FunctionDim employeerhs As Employee = CType(rhs, Employee) Return IIf(employeerhs.salary > employeelhs.salary, True, False) |
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) End Class Dim i As Integer End SubFor i = 0 To sortArry.Length - 1 Dim j As Integer Next iFor j = i + 1 To sortArry.Length - 1 If gtMethod(sortArry(j), sortArry(i)) Then Next jDim temp As Object = sortArry(i) End IfsortArry(i) = sortArry(j) sortArry(j) = temp |
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> </table><td style="width: 100px">name</td> </tr><td style="width: 100px">salary</td> <tr> <td style="width: 100px"> </tr><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> <td style="width: 100px"> </tr><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> <td style="width: 100px; height: 21px;"> </tr><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> <td style="width: 100px"> </tr><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> <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> <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 End Namespace Class _Default Inherits System.Web.UI.Page End ClassProtected 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 = "" End SubDim 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 iMe.lblResult.Text = employeelist.ToString() Public Class Employee Private name As String End ClassPrivate salary As Decimal Public Sub New(ByVal name As String, ByVal salary As Decimal) Me.name = name End SubMe.salary = salary Public Overrides Function ToString() As String Return String.Format(name + ":{0:C}", salary) End FunctionPublic Shared Function isGreater(ByVal lhs As Object, ByVal rhs As Object) As Boolean Dim employeelhs As Employee = CType(lhs, Employee) End FunctionDim employeerhs As Employee = CType(rhs, Employee) Return IIf(employeerhs.salary > employeelhs.salary, True, False) Public Class BubbleSorter Public Shared Sub Sort(ByVal sortArry() As Object, ByVal gtMethod As CompareOB) End ClassDim i As Integer End SubFor i = 0 To sortArry.Length - 1 Dim j As Integer Next iFor j = i + 1 To sortArry.Length - 1 If gtMethod(sortArry(j), sortArry(i)) Then Next jDim temp As Object = sortArry(i) End IfsortArry(i) = sortArry(j) sortArry(j) = temp |
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
No comments:
Post a Comment