Reexamining the Role of Classes and Objects


In Hour 2 we discussed the ideas behind object-oriented programming. To refresh your memory, object-oriented programming is a programming paradigm in which the object is a key construct of the programming language. Objects contain methods, properties, and events. Properties define the state of the object, and methods perform some action. Events commonly represent state changes or indicate that some action has transpired.

By the Way

Recall that in Hour 2 we described object-oriented programming using a car as an analogy. The properties of the car were such things as make, model, and color; its events were stepping on the brakes and turning on the windshield wipers; and its methods were drive, reverse, turn, and so on.

The list of properties, methods, and events that describe a car is referred to as a class, whereas an actual, concrete instance of a car, such as a 2002 green Honda Accord, is referred to as an object.


Classes are the abstractions from which objects are created. To understand the relationship between a class and an object, think of a calculator. The calculator may have properties like current battery power, current value on the screen, last operation entered, and others. It might have methods like add, subtract, and so on. Its events might include clearing the last computation and turning off. If you were to sit down and list all of the properties, methods, and events that a calculator has, this list would be equivalent to a class. This list is an abstract idea of what a calculator is and what it does. It clearly is not a concrete representation of a calculator; you cannot use the list to compute the product of 19.34 and 78.

An object, on the other hand, is a concrete representation of the class. The actual calculator that supports the properties, methods, and events outlined by the class is an object and is said to be an instance of the class it represents.

By the Way

To summarize, a class is an abstract definition, a simple list of properties, methods, and events that are supported. An object, however, is an instance of the class, a concrete "thing" whose properties we can set, whose methods we can call, and whose events can fire.


The Role of Objects in an ASP.NET Web Application

Recall from our earlier discussions that the .NET Framework contains a plethora of classes that allow for a variety of functionality. For example, each and every Web control that can be used in an ASP.NET page is represented by a class in the .NET Framework.

There are also classes in the .NET Framework that allow for an email to be sent from a web page, for data to be retrieved from a database, for an image to be created, and so on. The source code portion of your ASP.NET web page can utilize the variety of functionality present in the .NET Framework.

To use a class in the .NET Framework, we first must create an object from the particular class whose functionality we are interested in. Once we have an object, we may need to set some of the object's properties and call some of the object's methods. Additionally, we may want to create event handlers for some of the object's events.

As we will see starting with Hour 13, "An Introduction to Databases," ASP.NET's DataSource Web controls make it possible to retrieve and work with databases without having to write a lick of code. However, sometimes you still may need to programmatically access database data. While we won't be examining how to programmatically access databases in this book, understand that there are a number of database-related classes in the .NET Framework, such as SqlConnection, SqlCommand, and SqlDataReader.

The Four Common Tasks Performed with Objects

When using objects, we'll perform four tasks again and again:

  • Instantiation To work with an object, we'll need to create an instance of the object from the desired class. This is a required step that we must always do once when working with an object.

  • Setting property values Not all objects have properties, and we do not always need to use the properties of those that do. However, most of the time we'll find ourselves setting the properties of an object. Remember that properties are values that describe the state of the object. For example, a class that sends an email message might have properties like body, subject, to, from, cc, and so on.

  • Calling methods When using objects, we will always call one or more of the object's methods. A class that sends email messages might have a method called Send(), which would send the email. Therefore, to send an email from an ASP.NET web page using this class, we would first create an instance of the class; then set its body, subject, to, from, and other pertinent properties; and then call its Send() method.

  • Creating event handlers As with properties, not all objects have events, and we do not always need to use the events of those that do. Sometimes, however, we'll want to run a set of instructions only when a particular event for a particular object fires. To accomplish this, we'll need to create an event handler that's wired up to the object's pertinent event. We've seen examples of this in previous hours in creating event handlers for a Button Web control's Click event, or an event handler for the ASP.NET page's Load event.

In the remainder of this hour, we will look at the Visual Basic syntax required to accomplish these four tasks.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net