The projects we built in this section are Windows applications that contain a Class module. The class is contained within the project, and it’s used by the project’s main form. What if you want to use this class in another project?
First, you must change the type of the project. A Windows project can’t be used as a component n another project. Right-click the SimpleClass project and choose Properties. In the project’s Property Pages dialog box, switch to the Application tab, locate the Application Type drop-down list, and change the project’s type from Windows Forms Application to Class Library, as shown in Figure 6.5. Then close the dialog box. When you return to the project, right-click the TestForm and select Exclude From Project. A class doesn’t have a visible interface, and there’s no reason to include the test form in your project.
From the main menu, choose Build > Build SimpleClass (Please refer the example SimpleClass). This command will compile the SimpleClass project and create a DLL file (the file that contains the class’s code and the file you must use in any project that needs the functionality of the SimpleClass class). The DLL file will be created in the \bin\Release folder under the project’s folder.
Figure 6.5 – Setting a project’s properties through the Property Pages dialog box
Let’s use the SimpleClass.dll file in another project. Start a new Windows application, open the Project menu, and add a reference to the SimpleClass. Choose Project > Add Reference and switch to the Projects tab in the dialog box that appears. Click the Browse button and locate the SimpleClass.dll file (see Figure 6.6). Select the name of the file and click OK to close the dialog box.
Figure 6.6 – Adding a reference from an existing class to a new project
The SimpleClass component will be added to the project. You can now declare a variable of the SimpleClass.Minimal type and call its properties and methods:
Dim obj As New SimpleClass.Minimal
obj.BDate = #10/15/1992#
obj.property2 = 5544
MsgBox(obj.Negate())Code language: VB.NET (vbnet)
If you want to keep testing the SimpleClass project, add the TestForm to the original project (right-click the project’s name, choose Add > Add Existing Item, and select the TestForm in the project’s folder). Change the project’s type back to Windows Forms Application and then change its configuration from Release to Debug.