Modularity example

This note presents a modular programming example.

Our teaching notes have an Index page with links to the Topic Note containing each key (boldface) term. I wrote a program to construct that Index page automatically. The program scans through each Topic Note looking for boldface terms. When it finds them, it enters them into an array with a link to the corresponding Topic Note file. When it has them all, they are sorted into alphabetic order and written into the Terms.htm file you see on the Web site.

The user interface is very simple. When the user clicks the Build button, the file is created and the number of terms and Topic Notes displayed:

       

I could have written this as one large event handler that was executed when the user clicked the Build button, but that would have been confusing. Instead, I broke it into five small subprograms. The event handler for the Build button calls each of them in order:

Private Sub BtnBuild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBuild.Click
   Call GetFilenames ()      'place the names of each note file in the FileNames array
   Call FindTerms ()         'find the boldface terms in each file in FileNames and place them and the file name in Result
   Call SortResult ()        'sort Result into alphabetical order
   Call OutputResult ()      'create a Web page by writing the contents of Result to the file Terms.htm
   Call Close ()             'display the number of records and terms and close Terms.htm
End Sub
From this, you can see the overall program logic without getting lost in the details of how each subprogram works. You can focus your attention on what it does, not how it does it.

The subprograms communicate with each other by assigning values to four form-level global variables. (Their effects are to change the values of those variables).

The four global variables are:

Dim FileNames() As String    'path and name of each Topic Note file
Dim Result(1, 500) As String 'boldface terms and corresponding path and file names
Dim LastResult As Integer    'pointer to the last Result entry
Dim LastHTM As Integer       'pointer to the last FileNames entry
The following are short descriptions of each subprogram:
  1. GetFileNames. This subprogram gets the directory path and file name for each .htm file in the directory that holds the Topic Notes. It leaves them in the FileNames array. The subprogram also leaves a pointer to the last entry in the array in LastHTM.
  2. FindTerms. This subprogram searches each file in the FileNames array and finds boldface terms. Each of these is entered into the Results array, along with the path and name of the file in which it occurred. The first letters of each term are set to upper case. The subprogram also leaves a pointer to the last entry in the array in LastResult.
  3. SortResult. This subprogram sorts the Result array into alphabetical order.
  4. OutputResult. This subprogram writes the entries in the Result array to a file called Terms.htm. This is an HTML file ready for displaying on the Web. (You can see the file by clicking on the Index link on the Teaching Modules page and viewing its source listing).
  5. Close: This subprogram closes the output file Terms.htm and displays the number of terms and the number of notes that contained them. (These are in nextResult and LastHTM respectively).

Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.