Example for Week 10 - Step by Step


This example uses a Collection / Member Class pair to support an Add, Display, Update, Delete, and Browse application maintaining information about individuals.

Main uses the services of Display and the Collection Class module to perform this function. Very important is the separation of component responsibilities in this project.

Project

 

Main Display

The key to the Collection Class is the value in the ListBox.


The program will fail if you add a duplicate name

 

Description of the Classes

cPersons  is the Collection Wrapper. All it needs to do is implement the Collection Wrapper functions.

cPerson is the Member Class. Its responsibility is only to implement the Get and Let Property Procedures for each of the Class Member Variables

Download the Example


Main Form

The Main Form must create the Collection Class object when it starts, and destroy it when it is finished.

Private Sub Form_Load()
    Set m_Persons = New cPersons

End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set m_Persons = Nothing

End Sub

The Add, Display, Delete, Update, and Browse functions simply set an Action Code and Build the Display Screen. It must also maintain its own controls, and in this example involves only a ListBox that contains the Collection Class key value. There are unfortunatly two Build Sceen procedures. One of them when you know the Person you are going to display, and the other when you do not. If VB was more Polymorphic and supported Overloading, this code would be easier to implement

Private Sub BuildScreen()

    Dim Person As cPerson
    Set Person = m_Persons.Item(lstPerson.List(lstPerson.ListIndex))

    TextToScreen Person

    Set Person = Nothing

End Sub

Private Sub pBuildScreen(Person As cPerson)

    TextToScreen Person

End Sub

Private Sub TextToScreen(Person As cPerson)
    frmDisplay.txtName.Text = Person.Name
    frmDisplay.txtAddress.Text = Person.Address
    frmDisplay.txtCSZ.Text = Person.CSZ
    frmDisplay.txtPhone.Text = Person.Phone

End Sub

The Main Form uses the services of the Collection Class and the Display Form


Display Form

The Display is really just a traffic controller. It responds to the request from Main, and sets the Screen Attributes as is required by the request.

Public strMode As eMode
Public blnCancel As Boolean


The Request is made through strMode, which is of type eMode. strMode compares the request to the values allowed in the eMode definition. This makes the validation much cleaner.  What are the values of the components of eMode? It does not matter! That's exactly why this type is used!
Enum eMode
    eAdd
    eUpdate
    eDelete
    eBrowse
    eDisplay
End Enum

It's pretty easy to process the list of possible requests

Private Sub Form_Activate()
    Select Case strMode
        Case eDisplay
            LockScreen True
            cmdExit.Caption = "&Return"

        Case eAdd
            ClearScreen
            LockScreen False
            txtName.SetFocus
            cmdExit.Caption = "&Add"

        Case eUpdate
            LockScreen False
            txtName.Locked = True
            txtAddress.SetFocus
            cmdExit.Caption = "&Update"

        Case eDelete
            LockScreen True
            cmdExit.Caption = "&Delete"

        Case eBrowse
            LockScreen True
            cmdExit.Caption = "&Next"

    End Select

End Sub


Here are two procedures that use VB Collections to help manage the screen.

Private Sub ClearScreen()
    Dim cControl As Control

    For Each cControl In Me.Controls
        If TypeOf cControl Is TextBox Then
            cControl.Text = ""
        End If
    Next cControl

End Sub

Private Sub LockScreen(blnSet As Boolean)
    Dim cControl As Control

    For Each cControl In Me.Controls
        If TypeOf cControl Is TextBox Then
            cControl.Locked = blnSet
        End If

    Next cControl

End Sub


Collection Class

Upon entry, the Class must build a Collection. Remember this collection class is a wrapper that implements the Collection Methods and Properties.

Private mPersons As Collection

Private Sub Class_Initialize()
    Set mPersons = New Collection

End Sub

Private Sub Class_Terminate()
    Set mPersons = Nothing

End Sub

All that is left to do is call on the Collection services to support the wrapper functions. Only Add needs to know about the details of the Member Class.

Public Sub Add(ByVal strName As String, _
                            ByVal strAddress As String, _
                            ByVal strCSZ As String, _
                            ByVal strPhone As String)

    Dim Person As New cPerson

    With Person
        .Name = strName
        .Address = strAddress
        .CSZ = strCSZ
        .Phone = strPhone

        mPersons.Add Person, .Name

    End With

End Sub


Public Sub Remove(ByVal strName As String)
    mPersons.Remove strName

End Sub


Public Function Item(ByVal strName As String) As cPerson
    Set Item = mPersons.Item(strName)

End Function

Public Property Get Count() As Long
    Count = mPersons.Count

End Property

Each of these functions call the base Collection Class Methods.

Finally, there is the NewEnum Property Procedure required to support the Cpollection Class Enumeration. Note the Underscore in the code, and also make sure that you set the Procedure ID to -4 in the Procedure Attributes selection under the Tools menu item

Public Function NewEnum() As IUnknown
    Set NewEnum = mPersons.[_NewEnum]

End Function


Member Class

The Member Class only provides Get and Let Property Procedues for each of its exposed variables.

Private m_strName As String
Private m_strAddress As String
Private m_strCSZ As String
Private m_strPhone As String

This is what makes this model easy to support for any Member Class. This is really just a standalone class, and does not even know that it is being stored as part of a collection.


Back to Week 10