Creating Your Own Class Library


When you are first developing a new class, it's convenient to tie the new class code to the test program, as you have done in the TestPassword project. This allows you to test and debug the class in a fairly straightforward manner. However, the real benefit of classes is to be able to reuse the class objects in other programs. After all, code reuse is one of the primary goals of OOP. Therefore, you need a way to separate the CPassword class code from the test program, yet still make the class available for use in other programs. You can do this by creating a class library.

In order to create a class library, the first thing you need to do is copy the code associated with the CPassword class. To do this, you simply click the CPassword class Code window tab and move the cursor to the beginning of the code. Then you hold the left mouse button down and drag the cursor to the bottom of the Code window, highlighting all the code. Then you press Ctrl+C to copy the highlighted code. At this point, all the CPassword code is in the Visual Basic .NET copy buffer. Next, you close the TestPassword project.

To create your own class library, you need to select File, New, Project to display the New Project dialog box. Then click the Class Library option in the Templates pane of the window. In the Name field, type CPassword as the name for the class library. You should also enter a directory where you would like the library to reside. This is shown in Figure 15.5.

Figure 15.5. Naming a new class library by using the New Project dialog box.

graphics/15fig05.jpg

Next, you need to click the OK button to save the information. The screen is then updated to show the default Class Code window (see Figure 15.6).

Figure 15.6. The default Class Code window.

graphics/15fig06.jpg

As you can see in Figure 15.6, there is no code for the class yet. Visual Basic .NET has just created an empty Code window named Class1 that you can use to write the class code. However, because you placed the CPassword class code in the Visual Basic .NET copy buffer before you closed the TestPassword project, you can copy that code into the empty Code window. To do this, you highlight the two lines in the Class1 code window and press the Delete key to remove the two lines. Then you press Ctrl+V (or select Edit, Paste) to copy the CPassword class code into the Code window. You should then see all the class code in the Class1 Code window.

If the Solution Explorer window is not already open, you should select View, Solution Explorer (or press Ctrl+R) to open it. Then highlight the Class1.vb line and click the right mouse button. This opens a small menu from which you should select the Rename option. This allows you to rename the code from the generic Class1 name to something more meaningful. Type CPassword . The result is shown in Figure 15.7.

Figure 15.7. Renaming the generic Class1 code CPassword .

graphics/15fig07.jpg

You can see the renaming toward the right side of Figure 15.7. After you press Enter, the tab in the Code window changes from Class1.vb to CPassword.vb .

Finally, you need to select Build, Build Solution to compile the CPassword class code. This causes Visual Basic .NET to generate a dynamic link library (DLL) file named CPassword.dll . You now have a library file named CPassword that you can use in programs.

Using Library Files

How do you use the library file created in the preceding section? Actually, you already have seen how to use it. If you look at the top of Listing 15.6, you will see this statement:

 Imports CPassword 

This statement tells Visual Basic .NET to make the code in the CPassword file available for use in the current program. However, because the library file may not be located in the current working directory, you need to tell Visual Basic .NET where to find the CPassword file. To do this, you simply select Project, Add Reference, click the Browse button, and locate the file. (Your file should be located in the bin directory of the CPassword folder.) After you make this selection, the Imports statement knows where to look for the CPassword library file. This is all you have to do to make the CPassword class available for use in programs.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net