Deliverable 6: Polymorphism

Objectives: Use ArrayLists to demonstrate the polymorphic properties of objects..  

Description: This program is intended to provide you with experience using polymorphism, ArrayLists, and simple file I/O.

Allow the user to enter a series of references.  As each clsBook, clsJournal, or clsReference is entered, add it to an ArrayList of clsReferences.  (Research ArrayList, Collection, and Array to determine why ArrayList was deemed most appropriate for this project.)

Set up a form that includes radio buttons to allow the user to select the new format desired (MLA, APA, Chicago).  The program should also require the user to indicate where to save the formatted output using a file dialog.  (There is a demo here.) 

After the format is chosen and the file opened, the ArrayList should be sorted by first author's last name, first name, and middle initial, each element in the sorted ArrayList should have the proper formatting method (formatMLA, formatAPA, formatChicago) called, and the resulting string should be written to a new doc file.

Sorting an array or arrayList of clsReference objects will require that you modify clsReference to implement IComparable and to include the CompareTo method.



  1. Data entry
    1. enter reference details <from previous deliverable>
    2. store references in an ArrayList
  2. Format references
    1.  Specify destination file (.htm  file)
    2.  Select format
      1. loop through ArrayList, applying the selected format, and writing to htm file.  
      2. close the file
  3. View the htm file in a web browser control
  4. Copy the htm file to a doc file for use in a research paper

Interface Design

In the screens below the buttons perform the following functions:

Button Purpose
Radio buttons Create new clsBook, clsChapter, or clsReference, depending on the radio button.  Enable last, first, and middle initial fields, and add author, and save authors buttons.  Remove the tabs from the tab control (with the "remove" command), add the book tab, chapter tab, or journal tab with the "add" command, depending on the radio button. Disable the tab control (until the save authors button is clicked).
Save & Review Output Saves the reference ArrayList in formatted form in a file and displays it in a WebBrowser or RichTextBox control.
Submit Book Calls the routines to set the book title, publication date, publisher, city, state, and country. It then calls the routine to add the updated object to the reference ArrayList. It also resets the name counter, sets the reference pointer to Nothing, and calls a method to clear all book fields.
Submit Chapter Calls the routines to set the chapter title, publication month, day, and year, publisher, city, state, and country where the publisher is based, book title, and beginning page and ending page. It then calls the routine to add the updated object to the reference ArrayList. It also resets the name and editor counters, sets the reference pointer to Nothing, and calls a method to clear all chapter fields.
Submit Journal Calls the routines to set the paper title, the journal month, day, and year of publication, journal title, the beginning page and ending page, the journal volume and number . It then calls the routine to add the updated object to the reference ArrayList. It also resets the name counter, sets the reference pointer to Nothing, and calls a method to clear all journal fields.

Click here for some sample screenshots. 


VB Specifics:

Writing to file

Open the file using a SaveFileDialog.

Use PrintLine to write each line.

Include html tags to allow formatting, as in

PrintLine(fileNumber, "<html><body>")

We're being tricky.  From Word 97 to Word 2007 files with html tags are read as Word doc files if you use a .doc extension.  That changed in Word 2007.  So to save our formatted references and view them, we will save them in a .htm file.  In order to easily italicize our titles we simply enclose them in the html tags for italics, <i> and </i> and write them to a file that has as its first line <html><body>, and as its final line </body></html>.  Be sure to close your file as soon as you finish writing to it.  However, you can make a copy of the file with a .doc extension and it can later be inserted into your research paper, assuming it is in Word format.  (A .htm file can inserted into a .doc file too, but .doc files are easier to find since .doc is the default for Insert | File.)

File copy method

Private Sub copyFile(ByVal outfile as String)
    If System.IO.File.Exists(outFile) Then
        Dim newFileName As String = outFile.Substring(0, outFile.Length - 3) & "doc"

        ' if doc version of file exists, delete it because the copy
        ' command will NOT let you overwrite it!
        If System.IO.File.Exists(newFileName) Then
        End If

        ' copy htm file to a doc file
        System.IO.File.Copy(outFile, newFileName)
    End If
End Sub

WebBrowser Control

The review feature requires that you can use the WebBrowser control to display the doc file that was created by the application.  In this example the destination file name was saved in a form-level variable called outFile. The WebBrowser control allows you to view a doc file. Here is sample code for the sub:

Private Sub reviewRefs()

     Dim nullObject As System.Object = 0
     Dim str As String = ""
     Dim nullObjStr As System.Object = str
     Cursor.Current = Cursors.WaitCursor         ' see
comment below
     WebBrowser1.Url = New Uri(outFile)         
     Cursor.Current = Cursors.Default

End Sub

.Net 2005 documentation at MSDN indicates that the WebBrowser control is already in the toolbox by default.  If it is not, follow these directions:

Add the WebBrowser control to your toolbox:

  • Tools > Choose Toolbox Items...
  • In the COM Components tab select Microsoft Web Browser

That should add WebBrowser to the Common Controls section of your toolbox.

Add the control to the screen.

Killing Word (not needed if you use a .htm file instead of .doc)

If you try to use the same doc file twice you may get an error message telling you that the file is in use by another process.  That is because whenever the doc file is displayed in the WebBrowser control WINWORD is running as a process.  I had to write a sub to kill WINWORD and call it as the first line in the method for the Review Output click event.  It kills ALL instances of Microsoft Word.  Here is the code:

Add this line at the top of your program BEFORE any other line! 

Imports System.Diagnostics


Private Sub endWord()
Dim parr As Process() = Process.GetProcessesByName("WINWORD")
For i As Integer = 0 To parr.Length - 1

Note: You could also use a RichTextBox if you prefer instead of the WebBrowser control and html tags.  Here is a demo.

If you use a RichTextBox you will need to add a method called getRefSource to your IFormatReferences interface.  It should return the book title for a book, the book title for a chapter, and the source for a journal.  It will be used to italicize the reference source for those reference formats that require it.

NOTE: I prefer that you use the RichTextBox.

Cursor icon

If you have trouble with the version of the cursor command above, you can try

Me.Cursor = Cursors.WaitCursor
Me.Cursor = Cursors.Default

although you may need to refer to it as


Use a normal For loop to process your ArrayList.  If you use the For Each Next loop incorrectly in can "disable" polymorphism.

You should not need this (unless you ignore the previous comment), but remember that a line like

CType(ref, clsBook)

casts an object (in this case of clsReference) to a clsBook type.

A line like

If TypeOf ref Is clsBook

returns true if the type of the object ref is clsBook.