When creating Web applications within an organization,
sometimes you find yourself copying and pasting the same code from one
application to another. Ideally, you could consolidate all of these procedures
into a nice little package that you could utilize across your domains.

In the meantime, if you have procedures that you use over
and over, it might be helpful to create a class to contain your procedures.
There are advantages to organizing your code with classes: It makes your code
easier to read and debug; you can move your classes over to a Web service with
ease; you provide an abstract tool for other developers (which saves time and
money); and you remain flexible in a project’s development phase.

When organizing your common procedures into a class, you create
a level of abstraction that allows you to implement these procedures in almost
all of your code. For instance, say that in every Web application you build,
you need to initialize user information. This initialization includes making a
couple of trips to a database server to grab information based on the user’s
credentials when he or she logs in to your application. If you perform this task in every application, it makes sense to consolidate your code into a class.
Listing A
shows you how.

In this example, the UserName and UserPhone properties are available as soon as the class
initializes. You can place this class code at the bottom of the ASP source
code. Then, you can utilize the functionality within the rest of your code
without instantiating the ADO objects, making the database calls, and cleaning
up afterwards. Also, if you’re using Microsoft Visual InterDev, the properties
or methods you define in your class will be available through IntelliSense.

IntelliSense only works one level deep; so if you create a
class that returns an object of another ASP class, you will not receive the
methods and properties available to it.

This is the code of a class skeleton:

Class MyClass

Private Sub Class_Initialize()
End Sub

Private Sub Class_Terminate()
End Sub

End Class

With this basic information, a class is declared, and you
can create objects from it. However, the objects based from this class have no
functionality—they’re sort of useless. Methods for classes are built from
Public Sub or Public Function declarations (Subs return nothing, while
Functions return something).

There are two ways that properties can be exposed: either by
a Public VarName in the global context of the class
or by Public Property Get/Set/Let. If you just want read-only properties, you
simply use a Property Get declaration. All other Properties, Subs, or Functions
declared Private are applicable only within the instantiated object. Variables
declared Private within the global context of the class are member variables.

Another advantage to creating classes is that you can easily
move these class declarations over to Web services. Since this is the direction
many developers are going in, you can get a heads up on creating object-oriented
code by utilizing classes. When you move these class declarations over to a Web
service, you won’t have to rewrite all of the ASP code that you normally would
if you put the code inline. The only thing that will change is the
instantiation of your object.

One of the biggest advantages I find to creating this
abstraction is employing the help of other Web developers. Since it isn’t
productive for a handful of developers to create the same code over and over
again with each implementation being different, this approach provides an
abstract tool for each developer. This allows each developer to focus more on
the functionality of the current job rather than spending time developing the
same code that has already been developed.

I don’t provide a COM component to handle this functionality
because that locks you into a particular design. By creating the class
dynamically in ASP, you can make changes to the class without having to
recompile and redistribute. Once the class is solidified, move it to COM, a Windows
Script Component (WSC), or a Web service. But during the development stage,
it’s a good idea to keep things a bit flexible.

Keep your developer skills sharp by automatically signing up for TechRepublic’s free Web Development Zone newsletter, delivered each Tuesday.