Methods
With our dog example, we can make a particular dog do things by calling com-
mands. If I want Rayne to sit, I tell him to sit. If I want Rayne to lie down, I tell
him to lie down. In object oriented terms, I tell him what I want him to do by
calling a predefined command or method, and an action results. For example, to
make Rayne sit, we would use the following code to call his Sit method:
Visual Basic
rayne.Sit()
C#
rayne.Sit();
Given that rayne is an instance of our Dog class, we say that the Sit method is
exposed by the Dog class.
Classes
You can think of a class as a template for building as many objects of a particular
type as you like. When you create an instance of a class, you are creating an object
of that class, and the new object has all the characteristics and behaviors (prop-
erties and methods) defined by the class.
In our dog example, rayne was an instance of the Dog class, as Figure 3.6 illus-
trated. In our code, wed create a new instance of the Dog class called rayne, as
shown below:
Visual Basic
Dim rayne As New Dog()
C#
Dog rayne = new Dog();
Constructors
Constructors are special kinds of method are that used to initialize the object.
In OOP, when we create new instances of a class, we say were instantiating
that class. The constructor is a method of a class thats executed automatically
when a class is instantiated.
82
Chapter 3: VB and C# Programming Basics

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.