Creating Classes
Using objects is fairly straightforward and intuitive. It is
the kind of
thing that even the most novice programmers pick up and accept rapidly.
Creating classes and objects is a bit more complex and interesting, however,
and that is what we'll cover through the rest of the chapter.
Creating Basic Classes
As we discussed earlier, objects are merely instances of a
specific template (a class). The class contains the code that defines the
behavior of its objects, as well as defining the instance variables that will
contain the object's individual data.
Classes are created using the Class
keyword, and include definitions (declaration) and implementations (code) for
the variables, methods, properties, and events that make up the class. Each
object created based on this class will have the same methods, properties, and
events, and will have its own set of data defined by the variables in our
class.
The Class Keyword
If we wanted to create a class
that represents a person a Person
class we could use the Class
keyword like so:
Public Class Person
'
implementation code goes here
End Class
As we know, VB.NET projects are composed of a set of files
with the .vb
extension. Each file can contain multiple classes. This means that, within a
single file, we could have something like this:
Public Class Adult
'
implementation code goes here
End Class
Public Class Senior
'
implementation code goes here
End Class
Public Class Child
'
implementation code goes here
End Class
The most common approach is to have a single class per file.
This is because the VS.NET Solution Explorer and the code-editing environment
are tailored to make it easy
to navigate from file to file to
find our code. For instance, if we create a single class file with all these
classes, the Solution Explorer simply shows a single entry:
However, the VS.NET IDE does provide the Class View window. If we do decide to put multiple
classes in each physical .vb
file, we can make use of the Class View
window to quickly and efficiently navigate through our code jumping from
class to class without having to manually locate those classes in specific code
files:
The Class View
window is incredibly
useful even if we keep to one class per file, since it still provides us with a
class-based view of our entire application.
In this chapter, we'll stick with one class per file, as it is
the most
common approach. Open the VS.NET IDE and create a new Windows
Application project. Name it ObjectIntro.
Choose the Project | Add Class
menu option to add a new class module to the project.
We'll be presented with the standard Add New Item
dialog.
Change the name to Person.vb
and click Open.
The result will be the following code that defines our Person
class:
Public Class Person
End Class
It is worth noting that allVB.NET source files end in a .vb
extension, regardless of which
type of VB source file we choose (form, class, module, etc.) when we are adding
the file to our project. In fact, any forms, classes, components, or controls
that we add to our project are actually class modules they are just specific
types of classes that provide the appropriate behaviors. Typically, these
behaviors come from another class via inheritance, which we'll discuss in
Chapter 6.
The exception is
the Module,
which is a special construct that allows us to include code within our
application that is not directly contained within any class. As with previous
versions of Visual Basic, methods placed in a Module can be called directly
from any code within our project.
With our Person
class created, we're ready to
start adding code to declare our interface, implement our behaviors, and to
declare our instance variables.
Member Variables
Member or instance variables are
variables declared in our class that will be available to each individual
object when our application is run. Each object gets its own set of data
basically each object gets its own copy of the variables.
At the beginning of the chapter, we discussed how a class is
simply a template from which we create specific objects. Variables that we
define within our class are also simply templates and each object gets its
own copy of those variables in which to store its data.
Declaring member variables is as easy as declaring variables
within the Class
block structure. Add the following code to our Person
class:
Public Class Person
Private mstrName As String
Private mdtBirthDate As Date
End Class
We can control the scope of our variables by using the
following keywords:
q
Private
available only to code within our class
q
Friend
available only to code within our project/component
q
Protected
available only to classes that inherit from our class discussed in detail
in Chapter 6
q
Protected Friend
available to code within our project/component and classes
that inherit from our class whether in our project or not discussed in detail
in Chapter 6
q
Public
available to code outside our class
Typically, member variables are declared using the Private
keyword making them available only to code within each instance of our class.
Choosing any other option should be done with great care, as all the other
options allow code outside our class
to directly interact with the variable meaning that the value could be
changed and our code would never know that a change took place.
One common
exception to making variables Private
is the use of the Protected
keyword, as we'll discuss in Chapter 6.
Methods
Objects typically need to provide services (or functions) that
we can call when working with the object. Using their own data, or data passed
as parameters to the method, they manipulate information to yield a result or
to perform a service.
Methods declared as Public, Friend, or Protected in scope
define the interface of our class. Methods that are Private in scope are only available to the code within the
class itself, and can be used to provide structure and organization to our
code. As we discussed earlier, the actual code within each method is called implementation, while the declaration of
the method itself is what defines our interface.
Methods are simply routines that we code within the class to
implement the services that we want to provide to the users of our object. Some
methods return values or provide information back to the calling code. These
are called interrogative
methods. Others, called imperative methods,
just perform a
service and return nothing to the calling code.
In VB.NET, methods are implemented using Sub
(for imperative methods) or Function
(for interrogative methods)
routines within the class module that defines our object. Sub
routines may accept parameters, but they don't return any result value when
they are complete. Function routines can also accept parameters, and they always
generate a result value that can be used by the
calling code.
A method declared with the Sub
keyword is merely one that returns no value. Add the following code to our Person
class:
Public Sub Walk()
'
implementation code goes here
End Sub
The Walk
method would presumably contain
some code that performed some useful work when called, but has no result value
to return when it is complete.
To use this method, we might write code such as:
Dim myPerson As New Person()
myPerson.Walk()
Once we've created an instance of the Person
class, we can simply invoke the Walk
method.