Testing the Classes
Because you created this project as a class library, you can’t test it by simply clicking the Run button. Instead, you need to create a new solution that contains multiple projects.One effective way of testing the classes in a class library is to create a “dummy” project that can use the test classes. Creating an empty or nearly empty test project allows you to focus on debugging the code in your class library instead of trying to simultaneously work with code both inside and outside of the class library. Only when you’ve gotten your class library classes working as desired should you use the classes in a real project.To create a new test application, select File



Figure 2-6: Creating a multiproject solution by adding a project to an existing solution
The next step is to create a reference in the tester project to the DicePanel project. To do this, right-click References in the tester project (make sure you get the correct project now that you have two of them) and select Add Reference. Then, select the Projects tab and select the DicePanel project.Once you add the reference, you can create and use the DicePanel class in your projects. Listing 2-24 shows the code that makes up the tester project, which creates a DicePanel and links it up to roll when clicked.Listing 2.24: The Tester Project
Public Class Form1
Inherits System.Windows.Forms.Form
#Windows Form Designer generated code
Private d As DicePanel.DicePanel.DicePanel
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
d = New DicePanel.DicePanel.DicePanel()
With d
.Dock = DockStyle.Fill
.Height = Me.Height - 32
.NumDice = 3
.DebugDrawMode = True
End With
AddHandler d.Click, AddressOf ClickIt
Me.Controls.Add(d)
End Sub
Private Sub ClickIt(ByVal sender As Object, _
ByVal e As System.EventArgs)
d.RollDice()
End Sub
End Class
It might look a bit odd that the variable d is declared as DicePanel.DicePanel.DicePanel. The three words represent the assembly name (DLL name), the namespace, and then the class name. In hindsight, it might be better to name some of these things differently or perhaps even remove the namespace from inside the assembly (which would probably require “fixing” the bitmap loading code because it refers to the assembly/namespace names). The rest of the code creates the DicePanel instance, sets a few properties, and adds it to the form’s Controls collection (a required step whenever you add visual controls to a form at runtime). The AddHandler statement “wires up” the Click event to a procedure named ClickIt, which calls the RollDice method on the DicePanel instance.Now that you’ve set up the two projects in a single solution and linked them via their references, you can set breakpoints in the class library and do “live” debugging to get your classes working.