Results 1 to 1 of 1

Thread: [HOW TO] Create your own Strongly Typed Collections

  1. #1
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default [HOW TO] Create your own Strongly Typed Collections

    Most ASP.NET websites will use some form of data that is needed to be displayed. It is easy enough to place all of this data into seperate variables, or even better, a series of arrays, but there is a much better way of organising this data within the back end of your site; by using Strongly Typed Collections.

    There are many advantages of STCs over using multi-dimensional arrays, both from a safety and aesthetical view.

    I will be working in VB.NET throughout this guide.

    The first stage to creating an STC is to setup a Class Object. For this example I will create a very basic Class Object with only three private members and a few simple properties and an override method for Object.ToString().

    PHP Code:
    Option Explicit On
    Option Strict On

    Imports Microsoft
    .VisualBasic
    Imports System
    .Net.Mail

    Namespace CustomClasses
        
    Namespace Objects
            
    Public Class Person

    #Region "Private Members"
                
    Private m_firstName As String
                
    Private m_lastName As String
                
    Private m_emailAddress As MailAddress
    #End Region

    #Region "Constructors"
                
    Public Sub New(ByVal _firstName As StringByVal _lastName As StringByVal _emailAddress As String)
                    
    m_firstName StrConv(_firstNameVbStrConv.ProperCase)
                    
    m_lastName StrConv(_lastNameVbStrConv.ProperCase)
                    
    m_emailAddress = New MailAddress(_emailAddressMe.FullName)
                
    End Sub

                
    Public Sub New(ByVal _fullName As StringByVal _emailAddress As String)

                    
    ' Split _fullName into _nameArray ( 0 = _firstName | 1 = _lastName) '
                    
    Dim _nameArray As String() = _fullName.Split(" c")
                    
    Dim _firstName As String _nameArray(0)
                    
    Dim _lastName As String _nameArray(1)

                    
    ' Define private members. '
                    
    m_firstName StrConv(_firstNameVbStrConv.ProperCase)
                    
    m_lastName StrConv(_lastNameVbStrConv.ProperCase)
                    
    m_emailAddress = New MailAddress(_emailAddressMe.FullName)
                
    End Sub
    #End Region

    #Region "Public Properties"
                
    Public ReadOnly Property Email As MailAddress
                    Get
                        
    Return m_emailAddress
                    End Get
                End Property

                
    Public ReadOnly Property FirstName As String
                    Get
                        
    Return m_firstName
                    End Get
                End Property

                
    Public ReadOnly Property FullName As String
                    Get
                        
    Return String.Format("{0} {1}"m_firstNamem_lastName)
                    
    End Get
                End Property

                
    Public ReadOnly Property LastName As String
                    Get
                        
    Return m_lastName
                    End Get
                End Property
    #End Region

    #Region "Methods"
                
    Public Overrides Function ToString() As String
                    
    Return Me.FullName
                End 
    Function
    #End Region

            
    End Class
        
    End Namespace
    End Namespace 
    Now that we have a ClassObject, we need to create a custom CollectionClass for it. The key here is the line Inherits CollectionBase. This is what enables us to overlay our own custom methods and properties onto a .NET recognised Collection. In reality, we pass our custom values into the base Methods for a standard Collection.

    In this example, I have added methods for Add, Remove, IndexOf and Contains, but other methods can be added to Sort and Enumerate the Collection.

    PHP Code:
    Option Explicit On
    Option Strict On

    Imports Microsoft
    .VisualBasic
    Imports System
    .Collections
    Imports System
    .Collections.Generic

    Namespace CustomClasses
        
    Namespace Collections
            
    Public Class PersonCollection
                Inherits CollectionBase

    #Region "Properties"
                
    Default Public Property Item(ByVal Index As Integer) As Objects.Person
                    Get
                        
    Return CType(List.Item(Index), Objects.Person)
                    
    End Get
                    Set
    (ByVal Value As Objects.Person)
                        List.
    Item(Index) = Value
                    End Set
                End Property
    #End Region

    #Region "Methods"
                
    ' Adds a new Person to the collection. '
                
    Public Function Add(ByVal Item As Objects.Person) As Integer
                    
    Return List.Add(Item)
                
    End Function

                
    ' Return True if the collection contains this Person. '
                
    Public Function Contains(ByVal value As Objects.Person) As Boolean
                    
    Return List.Contains(value)
                
    End Function

                
    ' Return this Persons index. '
                
    Public Function IndexOf(ByVal value As Objects.Person) As Integer
                    
    Return List.IndexOf(value)
                
    End Function

                
    ' Remove a Person from the collection. '
                
    Public Sub Remove(ByVal value As Objects.Person)
                    List.
    Remove(value)
                
    End Sub
    #End Region
            
    End Class
        
    End Namespace
    End Namespace 
    We can place all of this within one single CustomClasses.vb file as so:

    PHP Code:
    Option Explicit On
    Option Strict On

    Imports Microsoft
    .VisualBasic
    Imports System
    .Net.Mail
    Imports System
    .Collections
    Imports System
    .Collections.Generic

    Namespace CustomClasses
        
    Namespace Objects
            
    Public Class Person

    #Region "Private Members"
                
    Private m_firstName As String
                
    Private m_lastName As String
                
    Private m_emailAddress As MailAddress
    #End Region

    #Region "Constructors"
                
    Public Sub New(ByVal _firstName As StringByVal _lastName As StringByVal _emailAddress As String)
                    
    m_firstName StrConv(_firstNameVbStrConv.ProperCase)
                    
    m_lastName StrConv(_lastNameVbStrConv.ProperCase)
                    
    m_emailAddress = New MailAddress(_emailAddressMe.FullName)
                
    End Sub

                
    Public Sub New(ByVal _fullName As StringByVal _emailAddress As String)

                    
    ' Split _fullName into _nameArray ( 0 = _firstName | 1 = _lastName) '
                    
    Dim _nameArray As String() = _fullName.Split(" "c)
                    
    Dim _firstName As String _nameArray(0)
                    
    Dim _lastName As String _nameArray(1)

                    
    ' Define private members. '
                    
    m_firstName StrConv(_firstNameVbStrConv.ProperCase)
                    
    m_lastName StrConv(_lastNameVbStrConv.ProperCase)
                    
    m_emailAddress = New MailAddress(_emailAddressMe.FullName)
                
    End Sub
    #End Region

    #Region "Public Properties"

                
    Public ReadOnly Property Email As MailAddress
                    Get
                        
    Return m_emailAddress
                    End Get
                End Property

                
    Public ReadOnly Property FirstName As String
                    Get
                        
    Return m_firstName
                    End Get
                End Property

                
    Public ReadOnly Property FullName As String
                    Get
                        
    Return String.Format("{0} {1}"m_firstNamem_lastName)
                    
    End Get
                End Property

                
    Public ReadOnly Property LastName As String
                    Get
                        
    Return m_lastName
                    End Get
                End Property
    #End Region

    #Region "Methods"
                
    Public Overrides Function ToString() As String
                    
    Return Me.FullName
                End 
    Function
    #End Region

            
    End Class
        
    End Namespace

        Namespace 
    Collections
            
    Public Class PersonCollection
                Inherits CollectionBase

    #Region "Properties"
                
    Default Public Property Item(ByVal Index As Integer) As Objects.Person
                    Get
                        
    Return CType(List.Item(Index), Objects.Person)
                    
    End Get
                    Set
    (ByVal Value As Objects.Person)
                        List.
    Item(Index) = Value
                    End Set
                End Property
    #End Region

    #Region "Methods"
                
    ' Adds a new Person to the collection. '
                
    Public Function Add(ByVal Item As Objects.Person) As Integer
                    
    Return List.Add(Item)
                
    End Function

                
    ' Return True if the collection contains this Person. '
                
    Public Function Contains(ByVal value As Objects.Person) As Boolean
                    
    Return List.Contains(value)
                
    End Function

                
    ' Return this Persons index. '
                
    Public Function IndexOf(ByVal value As Objects.Person) As Integer
                    
    Return List.IndexOf(value)
                
    End Function

                
    ' Remove a Person from the collection. '
                
    Public Sub Remove(ByVal value As Objects.Person)
                    List.
    Remove(value)
                
    End Sub
    #End Region
            
    End Class
        
    End Namespace
    End Namespace 
    Once this is all set up, you can use it as so:

    PHP Code:
    Option Explicit On
    Option Strict On

    Imports Microsoft
    .VisualBasic
    Imports CustomClasses
    .Collections
    Imports CustomClasses
    .Objects

    Class Demo

        
    Public Shared Sub Main()

            
    Dim People As New PersonCollection

            People
    .Add(New Person("Joe Bloggs""j.bloggs@host.com"))
            
    People.Add(New Person("Davy""Jones""dj@locker.com"))

            For 
    Each P As Person In People
                Console
    .WriteLine(P.FullName "'s email address is " P.Email.Address)
            
    Next

        End Sub

    End 
    Class 
    Last edited by ApacheTech; 05-13-2012 at 10:31 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •