| Create a Class Library |
|
Objectives |
| Discussion |
Creating a Class Library
Referencing a Class Library in Another Project
| Demonstration |
In this demo we will create a Class Library for the rectangle class that we created in the last project. We will call the library MyRectangleLibrary. Then we will reference this library in another project and use the library.
1. Create a new class library project by selecting File...Project and selecting the Class Library template. Name the project MyRectangleLibrary.
2. Now there will be a class file already in the project. You could add code to this class file. But we are going to use the class file that we created in the last lesson - MyRectangle . Now we will add the MyRectangle class to this project. Select Project...Add Existing Item, find the MyRectangle class file (MyRectangle.vb) and add it.
3. Now right click the project name in Solution Explorer and select Rebuild. You should get an error because we need to add references so that the Form and Graphics classes are recognized. Right click References in the Solution Explorer and select Add References. In the Add Reference window that appears scroll through the .NET components and add System.Drawing and System.Windows.Forms.
4. Now we need to Import these Namespaces into MyRectangle.vb. Add the following two statements at the VERY top of the MyRectangle.vb code window.
Imports System.Windows.Forms
Imports System.Drawing
5. Now rebuild. Hopefully you don't get any errors.
6. Now we will create another project in this same solution so that we can actually use this library. Add a new Windows Application Project to this solution by clicking File...New Project and selecting the Windows Application template. Name your project "testMyRectangle". Make sure that you click the radio button Add to Solution. You want a solution with two projects in it: MyRectangleLibrary and testMyRectangle.
7. Make the testMyRectangle project the startup project for the solution by right-clicking on it in Solution Explorer and choosing Set As Startup Project.
8. We still need to add MyRectangleLibrary as a reference in testMyrectangle. Right-click on testMyRectangle in the Solution Explorer and select Add Reference. Then browse for MyRectangleLibrary.dll which is in the bin folder of the MyRectangleLibrary project.
9. We will make this program work just like the demo in the last lesson. Add a button (btnNewRectangle) and textbox (txtSide) to Form1 in testMyRectangle. At the top of the form under the Inherits statement add the following code to declare a rectangle object. Notice that we need to refer to the MyRectangleLibrary namespace to use the MyRectangle class. We can Import the MyRectangleLibrary namespace if we prefer.
Dim rectX As MyRectangleLibrary.MyRectangle
10. In the click event for the button add the following code. This code creates an instance of MyRectangle and displays the area of it. We are going to create a rectangle with equal sides so we will call the constructor that accepts the one parameter. Also we will pass the value of the textbox into the constructor.
rectX = New MyRectangleLibrary.MyRectangle(txtSide.Text)
MessageBox.Show(rectX.calcArea)
11. Now add the following to the MouseDown event for the form. This code will cause the rectangle to be drawn on the form where the user clicks.
rectX.DisplayRectangle(Me, e.X, e.Y)
12. Run the program and experiment.
| Exercises |
1. Create a Class Library for your BullsEye Class that you created in the last lesson.
2. Create a program that uses the BullsEye Class Library like we did in the demonstration above for MyRectangle.
| Links & Help |