Advertisements
RSS

Tag Archives: Collection

VB.NET Sort Method for a Collection Class


I have a VB.NET collection class EmployeesCollection for an Employee Class that got an Integer Property named Age; I want to create a method (SortByAge) to sort the collection members by Age.

 


This is the EmployeesCollection Class:

 

Imports Microsoft.VisualBasic

Public Class EmployeesCollection
Inherits System.Collections.CollectionBase

Public Sub Add(ByVal tblEmployee As Employee)
' Invokes Add method of the List object to add a widget.
List.Add(tblEmployee)
End Sub

Public ReadOnly Property Item(ByVal index As Integer) As Employee
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the Widget type, then returned to the
' caller.
Return CType(List.Item(index), Employee)
End Get
End Property

End Class
 


To create the SortByAge method I need a SortHelper Class, This helper Class will include a Function called Compare to compare between the Age values of the employees, the SortHelper Class will be part of the EmployeesCollection Class.

 

 
Private Class AgeSortHelper
        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
             Implements System.Collections.IComparer.Compare
            If x.Age > y. Age Then
                Return 1
            End If

            If x. Age < y. Age Then
                Return -1
            End If

            Return 0
        End Function
    End Class

 


Now we need to create the SortByAge method for the EmployeesCollection Class, in this method we will use the AgeSortHelper to sort the Employees.

 

    Public Sub SortByAge()
        Dim sorter As System.Collections.IComparer = New AgeSortHelper()
        InnerList.Sort(sorter)
    End Sub

 

This is the Final EmployeesCollection Class:

 

Imports Microsoft.VisualBasic

Public Class EmployeesCollection
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal tblEmployee As Employee)
        ' Invokes Add method of the List object to add a widget.
        List.Add(tblEmployee)
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As Employee
        Get
            ' The appropriate item is retrieved from the List object and
            ' explicitly cast to the Widget type, then returned to the
            ' caller.
            Return CType(List.Item(index), Employee)
        End Get
    End Property

    Public Sub SortByAge()
        Dim sorter As System.Collections.IComparer = New AgeSortHelper()
        InnerList.Sort(sorter)
End Sub
   
Private Class AgeSortHelper
        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
             Implements System.Collections.IComparer.Compare
            If x.Age > y. Age Then
                Return 1
            End If

            If x. Age < y. Age Then
                Return -1
            End If

            Return 0
        End Function

    End Class
End Class

Advertisements
 
Leave a comment

Posted by on August 4, 2009 in Programming

 

Tags: , , , , , , , , ,

C#:Using Dictionary as DropDownList Data Source

I have IDictionary Object called ListOptions. To use it as the data source for an ASP.NET DropDownList called objDropDownList:

 

objDropDownList.DataSource = ListOptions;

objDropDownList.DataTextField = “Value”;

          objDropDownList.DataValueField  = “Key”;

          objDropDownList.DataBind(); 

 

 

 

 
Leave a comment

Posted by on March 31, 2009 in Programming

 

Tags: , , , , , , , , , , , , , ,

How to sort a collection to be used in DropDown List

I have a collection object (EmployeesCollection) for custom Employee class, In the Employee class there are two properties Name and EmployeeID. I want to sort the EmployeesCollection by Name and use it in a DropDown List (ddEmployees).

 

DataTable dtEmployees = new DataTable();

          dtEmployees.Columns.Add(“Name”);

          dtEmployees.Columns.Add(“EmployeeID”);

          foreach (Employee vEmployee in EmployeesCollection)

          {

                    dtEmployees.Rows.Add(new object[] { vEmployee.Name, vEmployee. EmployeeID  });

          }

          dtEmployees.DefaultView.Sort = “Name”;

          ddEmployees.DataSource = dtEmployees;

          ddEmployees.DataTextField = “Name”;

          ddEmployees.DataValueField = “EmployeeID”;

          ddEmployees.DataBind();

 

 

 

 

 
Leave a comment

Posted by on February 18, 2009 in Programming

 

Tags: , , , , , , , , , , , ,

 
%d bloggers like this: