Car, for example. If you did, Car would be the class, or the type, and we could
create as many Car objects as we wanted, calling them myCar, johnsCar, davesCar,
and so on.
The class defines the behavior of all objects of that type. So all objects of type
Car will have the same behavior—for example, the ability to change gear. However,
each individual Car object may be in a different gear at any particular time; thus,
each object has its own particular state.
Let’s take another example: think of Integer (or int) as a class, and age and
height as objects of type Integer. The class defines the behavior of the ob-
jects—they’re numeric, and we can perform mathematical operations on
them—and the instances of objects (age and height) have their behavior defined
by the class to which they belong, but they also hold state (so age could be 20).
Take a look at the following code:
Visual Basic
Dim age As Integer
Dim name As String
Dim myCar as Car
Dim myOtherCar as Car
C#
int age;
string name;
Car myCar;
Car myOtherCar;
As you can see, the syntax for declaring an object is the same as that for declaring
a simple integer or string variable. In C#, we first mention the type of the object,
then we name that particular instance. In VB, we use the Dim keyword.
Object oriented programming sounds like an advanced topic, but getting started
with it is actually very easy, because OOP offers us a natural way to conceive and
design programs. Instead of writing long functions of code to perform specific
tasks, OOP allows us to group pieces of related functionality into classes that we
can reuse over and over, or even extend to incorporate new features. In OOP,
one thinks of programming problems in terms of objects, properties, and methods.
And, as we’ve seen, the best way to get a handle on these terms is to consider a
real-world object and imagine how it might be represented in an OOP program.
For the examples that follow, we’ll use as our example my dog, an Australian
Shepherd named Rayne.
78
Chapter 3: VB and C# Programming Basics