|4. Interfaces and the FCL Lab|
Overview: in this lab you'll build a database application to display attendee information, and then you'll work with interfaces in order to sort the attendees in various ways. The underlying database is an MS Access 2000 database names "Attendees.mdb"; you can view the contents of this database by double-clicking on the .mdb file in the work directory if you have MS Access installed.
Part 1: Attendee Database Application Design
Above is a screen shot of the WinForms application you will be working with. Attendees are displayed in the list box, initially sorted by full name (lastname, firstname). Attendee email and institution information is displayed in the text boxes. The option buttons control the order in which attendees are listed in the list box: by full name, last name, first name, or email address.
The design of the application is component-based, in that there exists a Data Access component (DLL) as well as a GUI front-end component (EXE). The application has been partially implemented for you, and as a result, there are two Visual Studio projects for you to work with (one for each component). To make things easier, note that Visual Studio supports the notion of a "solution", i.e. a solution may contain 1 or more projects that you are working on. This makes is easier to switch back and forth between the different components of your application when doing component-based development.
The Visual Studio solution (.sln) file, and underlying project (.csproj) files, have already been setup for you. The appropriate .sln file can be found in the work directory for this lab assignment (which is under the lab directory "5-Interfaces-FCL"). To open the solution, you can double-click on the .sln file, or startup Visual Studio and click Open Project.
Take a moment to browse the current design. The Data Access component, which resides in the DataAccessComponent namespace, defines two classes, Attendee and DataAccess. The DataAccess class communicates with the main form by returning an ArrayList of Attendee objects, which is then stored as a private member of the main form. The main form then traverses the ArrayList object to display attendees in the list box, resort the attendees, etc.
Compile the DataAccessComponent project by clicking on DataAccessComponent in the Solution Explorer window, and then using the Build menu to build just this component. It should successfully compile. Now click on AttendeesDBApp in the Solution Explorer window, and try to build that component. This will fail. Why? Essentially, the compiler is complaining that it never heard of the class DataAccess, nor Attendee. When this happens, the first thing to do is set a reference to the given component. When that component is another VS project open in the current solution, right-click on references, select Add Reference..., click on the Projects tab, and add a reference to the DataAccessComponent.
Try again to build the AttendeesDBApp. It will fail once again, but this time because the code is written assuming the DataAccessComponent namespace was imported. Import the namespace via using, and now AttendeesDBApp should compile without error.
Part 2: Database Access
Implement the static GetAttendees() method within the DataAccess class by opening the underlying MS Access 2000 database, creating an attendee object for each attendee record in the database, and adding these objects to an ArrayList data structure. Write the SQL so that the attendee records are sorted by full name (lastname, firstname), and thus added to the ArrayList in alphabetical order. The database consists of a single table named "Attendees", with 1 primary key "AID" (unique Attendee ID), and 4 string-based fields "FirstName", "LastName", "Email", and "Institution".
Run and test. The application should now display the attendees in the list box, in alphabetical order by full name.
Part 3: Interfaces and Sorting
Even though the attendees are already sorted by full name, we will need a way to put them back into this sorted order later. So modify the Attendee class to implement IComparable such that attendees are sorted by full name. Now modify the main form so that the radio button "Full Name" sorts the attendees by full name. Double-click on this radio button, and finish the implementation as follows: create an array of attendees from the form's private ArrayList member, and then sort this array via Array.Sort(). The existing code in the event method will then reload the GUI based on the contents of this array.
Run and test. Click on one of the other radio buttons to empty the list box, and then click on the "Full Name" radio button to resort the attendees and display the result. The list box should display the attendees once again in alphabetical order by full name.
If time permits...
While we may not have talked about this, note that Array.Sort() is overloaded. In particular, there is a version of Array.Sort() that takes a second parameter of type IComparer. In this case, the Sort() method sorts the array based on the values returned by this comparer object (sometimes called a functor since it is an object that contains no state and serves only to perform functional computation). This is how you sort the attendees by first name, last name, or email: you create a class for each sorting case, these classes implement IComparer, and then when it comes time to sort, you call Array.Sort() and pass it the array to sort as well as an instance of the class you want to use for your comparer. Read about the IComparer interface in the .NET Framework Documentation (see the System.Collections namespace), implement your three comparer classes in the AttendeesDBApp component, and then code the three radio buttons so that they properly sort by last name, first name, and email.
That's it, take a break!