Modeling a Real-World Object: Creating Your First Class
In this lesson, you will learn how to create a class using a Class Library project.
In the previous lesson, you learned that classes can be used as blueprints for objects that model real-world things. One of the best reasons for using classes is that once you have created a class for a certain type of object, you can reuse that class in any project.
For example, many programs that you write might involve people—an address-book program for keeping track of your friends, a contact-manager program for your business contacts, or a program for tracking employees. Although the programs may be considerably different, the attributes that apply to a person would be the same. Every person has a name, an age, an address, and a phone number.
In this and the next few lessons, you will create a class that represents a person; you can save this class and use it in other programs that you write in the future.
Classes can be created in three ways—as a part of the code in a form module in a Windows Application project, as a separate class module added to a Windows Application project, or as a stand-alone Class Library project.
Creating Classes
You may have noticed in some of the earlier lessons that when you double-click a form and open the Code Editor, you see something like the following:
Public Class Form1
Private Sub Form1_Load...
End Sub
End Class
That's right—the form is actually a class, marked by Class and End Class statements, and any code that you enter between those two statements is part of the class. Although by default a form module contains only a single class, you could create additional modules by adding code below the End Class statement as follows:
Public Class Form1
' Form1 code here
End Class
Public Class MyFirstClass
' Your class code here
End Class
The drawback to creating classes this way is that they are available only within the project in which you created them. If you want to share a class with other projects, you'll want to put it in a class module.
Class Modules
A class module is a separate code file that contains one or more classes. Because it is a separate file, it can be reused in other projects. Class modules can be created in two ways—as a module added to a Windows Application project, or as a stand-alone Class Library project.
You can add a new class module to an existing project by selecting Class in the Add New Item dialog box, available from the Project menu. For the purpose of this set of lessons, you will be creating a stand-alone Class Library project.
Try It!
To create a Class Library project
On the File menu, choose New Project.
On the Templates pane, in the New Project dialog box, click Class Library.
In the Name box, type Persons and then click OK.
A new Class Library project opens, and the Code Editor displays the class module Class1.vb.
In Solution Explorer, right-click Class1.vb and select Rename, and then change the name to Persons.vb.
Notice that the name in the Code Editor also changed to Persons.vb.
On the File menu, choose Save All.
In the Save Project dialog box, click Save.
Tip
Rather than saving the project in the default location, you may want to create a directory where you can store all of your classes for reuse. You can enter that folder in the Location field of the Save Project dialog box before you save.
For now, keep the project open—you will add to it in the next lesson.
Next Steps
In this lesson, you learned how to create a class module. An empty class is of little use though—in the next lesson, you will learn how to add properties to your class.
Next Lesson: Adding Properties to a Class