The goal of this lesson is not to try and explain object oriented programming (OOP). There are entire
books and classes dedicated to that. Nor is it to try and convince you that OOP is any better or worse
then procedural programming. That's a debate I'm simply not going to get involved in. The purpose of this
lesson is to give the beginning ASP.NET developer a basic introduction to objects and
some sample code that illustrates creating, instantiating, and using an object from an ASP.NET web page.
Why Use Objects?
There are several reasons to use objects when programming. Every book or class on OOP will tell you that
using objects makes code easier to reuse, maintain, and extend, but objects can also make the code you write
easier to
understand. Instead of creating an array or collection containing information about a product that a user is
about to order from a web store, you can simply create a product object. Then instead of needing to
remember that the third array item
represents the product's price, you can simply check the price property of the object to get its price.
This is the main reason why I use objects when I'm writing code. Once you get used to using them...
objects can makes reading and thinking about the code much easier.
Defining An Object
Before you can build something, you generally need to have a plan on how to build it.
For example, before a building is constructed, an architect is hired to draft a blueprint.
The blueprint contains the information needed to construct the building.
The same is necessary when building an object in a computer. This blueprint is called
a class. You create a class in VB by using the class keyword. Below is a simple class
for creating a dog object.
Public Class Dog
Public Dim Name As String
Public Dim BirthDate As DateTime
Public Dim Weight As Integer
Public Sub New(Name As String, BirthDate As DateTime, Weight As Integer)
MyBase.New
Me.Name = Name
Me.BirthDate = BirthDate
Me.Weight = Weight
End Sub
Public Function Speak()
Speak = "Woof Woof!"
End Function
End Class
Instantiation
The code above doesn't actually do anything for us except define what a dog is for the computer.
A class tells the computer what properties an object has and what it can do. If we created a dog
using this blueprint, it
would have a name, a birth date, a weight and it would know how to speak. That's it.
The dog that we created would be what is called an instance of the dog class listed above.
That's why creating an object from a class is called instantiation. Here's a code listing
showing how you'd do this in VB:
Dim myDog As Dog
myDog = New Dog("Spot", New Date(2000, 11, 12), 65)
You'll notice that we create our dog the same way we'd create any other variable in VB. That's because
VB (and the .NET Framework) are all object oriented to begin with. So now that VB knows
about our dog class, creating a dog object is the same as creating an integer, string, or date object.
Playing With Our Dog
Setting properties and calling methods of our dog object is accomplished the same way it is
with any other object:
' Set the Breed property
myDog.Breed = "dalmation"
' Call the Speak method
lblMyDogSays = myDog.Speak()
You'll see more samples of playing around with the object in the sample code you can download
at the bottom of this lesson.
Enabling Reuse Via a Namespace
This is all well and good, but at the moment all this cool stuff doesn't help us much in the reuse
department because we can only use our dog object on pages on which we define the class. On other pages,
there is no way for VB/ASP.NET to know anything about our dog. We can solve this problem by placing our
object in a separate .vb file, creating a namespace to contain our object, compiling the new file, and
then adding the output to our application's /bin directory. This process makes it available to any page
in our ASP.NET application because ASP.NET is set to automatically watch the /bin folder and
use whatever it finds there. As a result, we can gain access to our class from any page within our
ASP.NET application by simply including the following line at the top of the page:
<%@ Import Namespace="Animal" %>
The name "Animal" is simply what I called the namespace in my .vb file:
Imports System
Imports Microsoft.VisualBasic
Namespace Animal
Public Class Dog
Public Dim Name As String
Public Dim BirthDate As DateTime
...
...
...
End Class
End Namespace
The compilation part is pretty easy. For VB the command will look something like this:
That assumes you're at a command line prompt in the directory containing the source file
and that the VB compiler is in your path. If not you'll need to adjust your commands appropriately.
After compiling, all you need to do is move the resulting object_namespace.dll file to your
application's /bin directory where it will automatically be picked up and available to be included
by your .aspx pages.
Get The Code
You can download the code from this lesson from here: objects.zip (3.5 KB).
It includes the dog class, some sample usage, and the alternate version that places the class into the animal
namespace and compiles it. I've included the source (object_namespace.vb) and compiled versions (object_namespace.dll)
in case you have any trouble compiling it.
A quick note... my naming conventions for these files are less then optimal. Something like animal.vb would be
much better. I'm only using the object prefix because the files go with my object lesson and it makes
things simpler for me. In the real world files should be named for what they are. Sorry about that...