Lab for Week 11 - Step by Step

 

Very Busy (VB) Mail Order

Download the Completed Lab


This lab is a scaled down version of the lab in the text book. The program implements the Random Access file to permanantly store the information entered, and also features some error handling examples when opening the file. This lab demonstrates a different approach to implementing a calss module as it is the Customer form and not the Main that uses the Customers Collection Class

This example extends its Collection Class Wrapper function to include additional Public Properties and Methods. This is done mostly as a work around due to some of the OOP limitaions of VB, but one Public Method does an end run on the standard for any Collection Class wrapper. There is some value in discussing why this is not the right way to implement this feature.

As with any class module, one of the objectives is to hide the details of the class from the client. Most all of the I/O is hidden in the class and a discussion of the exceptions follow.


Here is the user interface:

LUIMain.jpg (16225 bytes)

 

LUICust.jpg (14100 bytes)

 


Here is an example of what NOT to do in a Collection Class wrapper:

Public Sub SaveRecord(ByVal strKey As String)
    SetupRecord strKey
    WriteRecord strKey
End Sub

This Public Subroutine can be called from any event handler in the Customer Form. Calling this subroutine will store the information in a Collection Class Item in the Random Access file.  This bypasses the Collection Item Method processing entirely. I can only speculate that this was done because the author did not want to discuss the implementation of Item as a property.

Private Sub SetupRecord(strKey)
    Dim intRecNum As Integer

    intRecNum = Val(strKey)
    With m_Customers(strKey)
        m_udtCustomer.strCustomerId = .CustomerID
        m_udtCustomer.strLastName = .LastName
        m_udtCustomer.strFirstName = .FirstName
        m_udtCustomer.strAddress = .Address
        m_udtCustomer.strCity = .City
        m_udtCustomer.strState = .State
        m_udtCustomer.strZipCode = .ZipCode
        m_udtCustomer.strCustomerCode = .CustomerCode
        End With
End Sub


Private Sub WriteRecord(strKey)
    Dim intRecNum As Integer

    intRecNum = Val(strKey)
    Put #m_intFileNumber, intRecNum, m_udtCustomer

End Sub





' Copy each member of the base class 
' to the Disk I/O Area












' Now write it to disk

See the example from this week for a demonstration on how to implement the Item method as a set of Property Procedures in a Collection Wrapper Class

 

Take a moment to review these additional Public Properties:

As mentioned before, FileOpened and OpenNewFile appear to be a means to work around VBs inability to pass a parameter when initializing an instance of a class and also to interact with the user if the required file is not found.


Public Property Get FileOpened() As Boolean
    FileOpened = m_blnFileOpened
End Property

Public Sub OpenNewFile()
    Open m_strFilePath For Random As #m_intFileNumber Len = Len(m_udtCustomer)
End Sub

 

Exposing this property allows the user interface to store the disk file key in item data. There is no reason to do this as this information can easily be stored in a base class property and hidden from the user interface. In this example, the key is. in fact stored in a base class property. It is not used though.

Public Property Get HighestKey() As String
    HighestKey = m_strHighestKey
End Property


Back to Week 11