Example for Week 11 - Step by Step


This example uses a collection class to hide the details of the Random Access processing from the user interface. This is an enhancement to an example from last week. Note that there is very little change to the user interface forms from the previous example. A Key value was added to the base class of the collection. This hold the position in the Random Access file for this item in the collection. The collection wrapper was modified to perform updates to the file when an item is removed from the collection, or an item is added to the collection.

The significant change to frmMain involved the record updates, but this was forced as the class item must be updated as a whole instead of each base class property individually. To leave the code alone would have triggered three updates per collection item changed.

Note the Get and Set properties for the Item method and in the Class Initialization when the Collection is populated from the content of the disk file.

Download the Example

The only problem that I have with this example is the coding of the file name in the class. VB does not support a parameter being passed to the Class Initialization procedure.


The User Interface, same as Week 10

UIMain.jpg (6322 bytes) UIDisplay.jpg (12177 bytes)

 

This was the only change to the User Interface modules from last weeks example:

Private Sub cmdUpdate_Click()
    If lstPerson.ListIndex = -1 Then
        MsgBox "Select a person to update"
        Exit Sub
    End If

    With frmDisplay
        .strMode = eUpdate
        BuildScreen
        .Show vbModal
        If .blnCancel = True Then
            Exit Sub
        End If

    ' Added to Main for Disk I/O
    Dim Person As New cPerson
    Set Person = _
        m_Persons.Item(lstPerson.List(lstPerson.ListIndex))

    Person.Address = .txtAddress.Text
    Person.CSZ = .txtCSZ.Text
    Person.Phone = .txtPhone.Text

    Set m_Persons.Item(.txtName.Text) = Person

    Set Person = Nothing

    ' m_Persons.Item(.txtName.Text).Address = .txtAddress.Text
    ' m_Persons.Item(.txtName.Text).CSZ = .txtCSZ.Text
    ' m_Persons.Item(.txtName.Text).Phone = .txtPhone.Text

    End With
End Sub














' Needed to update the collection item as 
' a single unit instead of each base class
' property individually






' The Update is here using the Set statement



' This is from last week

The file is opened and the collection populated when the class is initialized

Private Sub Class_Initialize()
    Set mPersons = New Collection

    ' File contents into the Collection
    blnInitializing = True
    fnPerson = FreeFile
    Open GetFileName For Random _
               As fnPerson Len = Len(thisPerson)

    ReadRecords

    blnInitializing = False

End Sub

Private Sub Class_Terminate()
    ' For Record Processing
    Close #fnPerson

    Set mPersons = Nothing

End Sub

Private Sub ReadRecords()
    CurrentKey = 0
    Get #fnPerson, , thisPerson

    Do Until EOF(fnPerson)

        With thisPerson
            If Not .blnDeleted Then
                Add .strName, _
                        .strAddress, _
                        .strCSZ, _
                        .strPhone
            Else
                CurrentKey = CurrentKey + 1
            End If

        End With

        Get #fnPerson, , thisPerson
    Loop
End Sub

The Item processing had to be modified to handle a single update. Last weeks example implemented changes by updating the base class members directly. This is not practical when each update would force a write to disk.

Public Property Set Item(ByVal strName As String,         Person As cPerson)
    With thisPerson
        .strName = Person.Name
        .strAddress = Person.Address
        .strCSZ = Person.CSZ
        .strPhone = Person.Phone
        .blnDeleted = False
    End With

    Put #fnPerson, Person.Key, thisPerson

End Property
Public Property Get Item(ByVal strName As String) As cPerson
    Set Item = mPersons.Item(strName)

End Property


The Add and Remove methods were also modified to write the collection item to disk.

The key to this example is to see how the enhancement of permanent storage for the data had very little effect on the user interface modules. As the work is done at the class level, any form of permanent storage could be implemented and the user interface modules or class client does not change.


Back to Week 11