
Defining Classes
This book hasn’t emphasized the fact, but you’ve been working with classes since the very
beginning. The very first program you created in Lesson 1 included several classes such as
the program’s main form and some behind-the-scenes classes that help get the program run-
ning. Since then, you’ve used all kinds of control classes, the
MessageBox class, the Array
class, collection classes, and more. You can even treat primitive data types such as
int and
string as classes under some circumstances.
In this lesson you learn how to create your own classes. You learn how to define a class and
give it properties, methods, and events to make it useful.
WHAT IS A CLASS?
A class defines a type of object. It defines the properties, methods, and events provided by its
type of object. After you define a class, you can make as many instances of that class as you like.
For example, the
Button class defines the properties and behaviors of a button user interface
element. You can create any number of instances of
Buttons and place them on your forms.
You can think of a class as a blueprint for making objects. When you create an instance
of the class, you use the blueprint to make an object that has the properties and behaviors
defined by the class.
You can also think of a class as a cookie cutter. Once you’ve created the cookie cutter, you can
make any number of cookies that all have the same ...