A Recap of Objects

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Day 4.  Using ASP.NET Objects with C# and VB.NET


As discussed yesterday, objects are reusable pieces of code and classes are the definitions behind those objects. Yesterday you tried your hand at creating a simple Clock object.

The .NET Framework consists of many classes that define objects, and ASP.NET is free to use all of them. Of course, there are some objects that would never be used in a real-world ASP.NET page because they don't apply to ASP.NET, but you can use them nevertheless. Let's recap some concepts from yesterday.

Properties

Properties are variables that describe an object. Listing 4.1 shows the clock example from yesterday in C#.

Listing 4.1 The clock Class in C#
 1:    <script language="C#" runat="server"> 2:       public class Clock { 3:          public int Second; 4:          public int Minute; 5:          public int Hour; 6:          public static int ClockCounter = 0; 7: 8:          public void SetTime(int intSec, int intMin, 9:             int intHour) 10:          { 11:             Second = intSec; 12:             Minute = intMin; 13:             Hour = intHour; 14:          } 15:       } 16: 17:    </script> 

graphics/analysis_icon.gif

Lines 3, 4, and 5 specify three properties for your class: Second, Minute, and Hour. If you were building a car class, the properties could be Color, Make, and Model.

Methods

Methods are things you can do with objects, such as winding a clock or setting the time. In Listing 4.1, the SetTime method on line 8 sets the properties for the class. Methods for a car would include Accelerate and Stop.

Object Instances

graphics/newterm_icon.gif

Before you can use an object, you must create an instance of it. That is, you must create a variable that holds the object in memory. It's important to understand the difference between an actual object and an object instance.

Using the clock example, you know that a clock is a generic object. My Clock object is an instance, however, and I can set its time. Your Clock object would be another instance, with its own set of properties. Figure 4.1 shows two instances of the clock, with different properties.

Figure 4.1. Two instances of the same Clock object have different properties.

graphics/04fig01.gif

Using this analogy, it doesn't make much sense to set the minute hand for a generic clock object. Would that affect all clocks? Which clock would it set? Generally, you can only set properties and call methods for instances of objects. Let's look at an example in C#:

 Clock.Second = 41;  //would produce an error because we can't                     //modify the properties of an object Clock objClock = new Clock(); objClock.Second = 41;  //works because objClock is an instance 

The second set of commands is the correct way to set properties. First create an instance of the object, and then set its properties. In VB.NET, these lines would be as follows:

 dim objClock as New Clock objClock.Second = 41 

Static Members

Note that I said, "Generally, you can only set properties… for instances." There are ways to set properties or call methods directly from the object. For example, suppose you have a counter in your clock class that tells you how many clock instances have been created. This is a general property that doesn't belong with any specific instance it's "just there." Properties and methods of classes that don't belong to any specific instance are called static members and are declared with the static keyword (the same in both C# and VB.NET):

 public static int ClockCounter = 0; public static int AddClock() {    return ClockCounter + 1; } 

In your ASP.NET page, these could be referenced as follows:

 <%    Clock objClock = new Clock();    objClock.SetTime(4,6,7);    Clock.AddClock;    Response.Write(int.ToString(Clock.ClockCounter)); %> 

On the next-to-last line of the previous code snippet, you call the static method AddClock, which increments the total number of clocks. This method doesn't need to be called from an instance. On the next line, you've used two static members. ToString is a static method of the int object that converts an int into a string, and ClockCounter is your static property that tells you how many clocks there are.

You won't be creating static members very often in your own classes, but it's useful to know what they are because ASP.NET has a ton of them.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

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