CSCI-E237, Fall 2012
Homework Assignment 1
Due by Tues Sep 18, 2359 ET

.NET Framework, Part 1

To successfully complete this homework, you'll have to learn how to deal with COM components (and clean up after them), create reusable UI Widows Forms controls that use custom collections as well as utilize generics and learn how to work around some of the pains that custom controls have in the Visual Studio designer view. All of these problems are more common than you would think or wish. 

1. Create a new solution containing the current assignment number (as you will for all subsequent ones, without being told again). Change the version of both client and component to match that of the assignment number, in this case, 1.0.0.0. Change the assignment number in the title bar as well. Perform this step on all subsequent assignments for the remainder of this course.. (no credit, but -5 if you fail to do so on any assignment). You may find this easiest if you create a new empty solution, then copy each project folder into the solution folder, then add each existing project to the solution.

2. You wish you didn't have to deal with COM ever again, but that's not going to happen. We need to put the official police radio codes ("One-Adam-12, 211 in progress", see a list at http://www.pimall.com/nais/n.radio.code.html if you actually give a darn) into comboBox1 of the dispatcher application so the user can see a list of them when entering a new incident. Unfortunately, the piece of software containing the official police radio codes  is a COM object. The guy who wrote it committed suicide out of boredom just after destroying the source code, so you can't get him to port it to .NET, and there's no money to pay you to write a new one. You're stuck with using it. Download and install the COM component here. Register it on your system. (Don't be surprised if it requires a support DLL that you don't have and can't easily identify -- that's COM for you. Or not. That's COM sometimes as well.)  Create a runtime-callable wrapper for accessing this object, and create an instance of it. Oh, and he didn't document it either, you'll have to figure it out from the type library. What a ditz. Sorry, but that's life. Deal with it, or find another job. (15 pts)

3. The combo box for displaying the incidents is used by dispatchers who know the codes, but the callers will be describing it with words ("HELP! I'm reporting a FIRE!"). We want the conversion table to be in the user interface, not the dispatcher's head. So we want the combo box to be populated with strings that look like this: "incident name : code #", for example "Drunk in public : 647f". We want to be able to separate out the incident code later, so we need the name and code to be accessible separately via the combo box. Read the documentation about how list boxes (of which the list portion of the combo box is one) hold objects of any class, displaying the string produced by the object's ToString() method. Create a .NET class for holding each combo box entry, containing read-only string properties for string name and code. Add a constructor that accepts the runtime callable wrapper object you get from the COM server, but which doesn't retain a reference to the COM object. Override the ToString() of this class and return a display string you assemble from these pieces.  Decide whether or not your class should also contain a default empty constructor (one that accepts no arguments).  Implement your decision, and defend it in a note to your TA. (10 pts)

4. That COM component is really, really ugly, and you want it in your program for as little time as possible. Every time you get any kind of object from it, immediately copy the data you need out of that object and deterministically destroy it. You may NOT force a garbage collection to free all unused objects, you must explicitly find and release all that exist. When you've finished with all of them, call the Win32 API function CoFreeUnusedLibraries using Pinvoke (look it up) to flush the COM server DLL right out of the system. It's easy to see when your client program no longer contains the COM server DLL by using the Process Explorer utility program written by my friend Mark Russinovich, and available free from his web site http://technet.microsoft.com/en-us/sysinternals/default.aspx   (15 pts)

5. This combo box is actually a neat piece of functionality, and you'd like to reuse it in other places in this application, and even in other applications. That means that instead of having to patch up the code for it each time, you'd like to roll it into a reusable class. Derive a new class from ComboBox that implements the functionality of your ComboBox that holds the offense codes, the one you built in steps 1-3 here. To further aid in using it correctly, you want to ensure that the items that are put into the Items collection are of the correct type. The current ObjectCollection holds System.Object, which means you can stick anything into it, even if it's not the right thing. So in your derived class, somehow make it so that the programmer using the control can't put the wrong class of object into the items collection. Make the new Items collection work that same way as the old one, except that it only accepts and returns objects of the class that you wrote in step 3 of this assignment. Do not use the generic mechanism as discussed in class, write the code specifically to take that class. (20 pts)

6. Now that you're reusing the offense code combo box in your application, you've gotten to really like it. You'd like to reuse it all over the place, strongly typed, holding different classes in the Items collection (but only one class in any individual combo box -- that is, box 1 can hold class A, box 2 can hold class B, but you don't have to mix A and B in either box.) Derive a new class from ComboBox that accomplishes the same thing as the one in step 5, but this time you specify the type that the Items collection holds as a generic parameter. Now you have a strongly typed combo box that can hold anything. Pretty cool, eh? (20 points)

7. At least it is until you try to put one of these generic combo boxes onto a form in the Visual Studio designer.  Then it doesn't work so well. The VS designer chokes on the generic parameter. It compiles fine, links fine, runs fine, but won't work in the designer. Damn. And what the hell? These guys had five years to get the designer to work with generics, what were they spending their time on? The power of this approach is obvious, but the tools won't swallow it. Find a way to fix it, so that your generic combo box runs right and doesn't break the designer. You don't have to make the Items collection work correctly in the designer, but the rest of the control has to. Put as much of the code as you possibly can in the derived class, so that the application programmer who uses this class on a form can do the absolute minimum amount of work. (20 points) Warning: No non-ugly method of solving this problem has yet been found, although some are definitely uglier than others.