|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.
In the screens below the buttons perform the following functions:
Writing to file
Open the file using a SaveFileDialog.
Use PrintLine to write each line.
Include html tags to allow formatting, as in
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
copyFile(ByVal outfile as String)
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:
.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:
That should add WebBrowser to the Common Controls section of your toolbox.
Add the control to the
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!
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.
If you have trouble with the version of the cursor command above, you can try
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
You should not need this
(unless you ignore the previous comment), but remember that a line like
casts an object (in this case of clsReference) to a clsBook type.
A line like
returns true if the type of the object ref is clsBook.