c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

VBScript in a Nutshell


 

VBScript in a Nutshell

By Paul Lomax, Matt Childs & Ron Petrusha


Chapter Excerpt: VBScript Program Structure

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

    Printer Friendly Version

    The Class Construct

    You declare a class using the Class...End Class construct. The syntax of the Class statement is:

    Class classname
    

    where classname is the name you want to assign to the class. It must follow standard VBScript variable naming conventions.

    Classes can contain variables, properties, methods, and events. How many of these and of what types is completely up to you. It is possible to have an object that has no properties or methods and supports only the two default events, but it won't be a very useful class.

    To instantiate an object--that is, to create an instance of your class that you can use in your code--use the following syntax:

    Set oObj = New classname

    where oObj is the name you want to assign to your object variable (it again must follow standard VBScript variable naming conventions), and classname is the name of the class. The statement creates an object reference--that is, the variable oObj contains the address of your object in memory, rather than the object itself.

    Class Variables

    In addition to properties, methods (which are either functions or subroutines), and events (which are subroutines), the code inside a Class structure can include variable definitions (but not variable assignments). The variable definition can take any of the following forms:

    Dim varName1 [, varName2...]
    Private varName1 [, varName2...]
    Public varName1 [, varName2...]
    

    The variable name must once again follow standard VBScript variable naming conventions.

    The Dim, Private, and Public keywords indicate whether the variable is accessible outside of the class. By default, variables are public--that is, they are visible outside of the Class...End Class structure. This means that the Dim and Public keywords both declare public variables, while the Private keyword declares a variable that's not visible outside of the class.

    In general, it is poor programming practice to make a class variable visible outside of the class. There are numerous reasons for this, the most important of which is that you have no control over the value assigned to the variable (which is especially a problem when dealing with a weakly typed language like VBScript) and no ability to detect when the value of the variable has been changed. As a rule, then, all variables declared within your classes should be private.

    Class Properties

    Typically, class properties are used to "wrap" the private variables of a class. That is, to change the value of a private variable, the user of your class changes the value of a property; the property assignment procedure (called a Property Let procedure) handles the process of data validation and assigning the new value to the private variable. If the private variable is an object, use an object property assignment procedure (called a Property Set procedure) to assign the new property value to the private object variable. Similarly, to retrieve the value of a private variable, the user of your class retrieves the value of a property; the property retrieval procedure (called a Property Get procedure) handles the process of returning the value of the private variable.

    Read-only properties (which wrap read-only private variables) have only a Property Get procedure, while write-only properties (which are rare) have only a Property Let or a Property Set procedure. Otherwise, properties have a Property Get procedure and either a Property Let or a Property Set procedure and are read-write.

    The use of public properties that are available outside of the class to wrap private variables is illustrated in Example 2-5, which shows a simple class that defines a private variable, modStrType, and a two read-write properties, ComputerType and OperatingSystem, the latter of which is an object property. Normally, you would validate the incoming data in the Property Let and Property Set procedures before assigning it to private variables, although that hasn't been done here to keep the example as simple as possible.

    Example 2-5: Using Properties to Wrap Private Variables
    Class Computer
    
       Private modStrType
       Private oOS
     
       Public Property Let ComputerType(strType)
          modStrType = strType
       End Property
     
       Public Property Get ComputerType(  )
          ComputerType = modStrType
       End Property
     
       Public Property Set OperatingSystem(oObj)
          Set oOS = oObj
       End Property
     
       Public Property Get OperatingSystem(  )
          Set OperatingSystem = oOS
       End Property
     
    End Class
    

    Class Methods

    Methods allow the class to do something. There is no magic to methods: they are simply subroutines or functions that do whatever it is you wish for the object to do. For example, if we created an object to represent a laptop computer in a company's inventory, then we would like to have a method that reports the laptop's owner. Example 2-6 shows a class with such a method.

    Example 2-6: Creating a Class Method
    Class LaptopComputer
    private modOwner
     
    Public Property Let CompOwner(strOwner)
       modOwner = strOwner
    End Property
     
    Public Property Get CompOwner(  )
       CompOwner = modOwner
    End Property
     
    Public Function GetOwner(  )
       GetOwner = modOwner
    End Function
     
    End Class
    

    As with properties, you can use the Public and Private keywords to make methods available inside or outside of the class. In the previous example, the method and both properties are available outside of the class because they are declared as Public.

    Note that in Example 2-6, the Property Get procedure performs the same functionality as the GetOwner method. This is quite common: you often can choose whether you want to implement a feature as a property or as a method. In this case, you could define both property procedures to be private; then the only way for anyone to get the owner information from the object would be to invoke the GetOwner method.

    The GetOwner method is declared as a function because it returns a value to the calling code. You can write methods as subroutines as well. You would do this when the method that you are calling does not need to pass back a return value to the caller.

    Class Events

    Two events are automatically associated with every class you create: Class_Initialize and Class_Terminate. Class_Initialize is fired whenever you instantiate an object based on this class. The executing the statement:

    Set objectname = New classname
    

    causes the event to fire. You can use this event to set class variables, to create database connections, or to check to see if conditions necessary for the creation of the object exist. You can make this event handler either public or private, but usually event handlers are private--this keeps the interface from being fired from outside code. The general format of the Class_Initialize event is:

    Private Sub Class_Initialize(  )
    Initalization code goes here
    End Sub
    

    The Class_Terminate event is fired when the object goes out of scope, or when the object is set to Nothing. You can use this handler to clean up any other objects that might be opened or to shut down resources that are no longer necessary. Consider it a housekeeping event. This would be a good place to make sure that you have returned all memory and cleaned up any objects no longer needed. The general format of the Class_Terminate event is:

    Private Sub Class_Terminate(  )
    Termination code goes here
    End Sub
    

    Once again, the event handler can either be public or private, though ordinarily it's defined as private to prevent termination code from being executed from outside of the class.

    Note that the Class_Terminate event is fired only when every instance of an object is destroyed. So, for instance, in the code fragment:

    Dim oDog
    Set oDog = New CDog
    Set oDog2 = oDog
    Set oDog3 = oDog2
    'Do something here
    Set oDog = Nothing
    Set oDog2 = Nothing
    Set oDog3 = Nothing
    

    the event is fired only in response to the last statement, when oDog3 is set equal to Nothing. In this case, all three object variables hold references to the same object. It is only when the last reference is released that the event is fired.

    Printer Friendly Version

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Level:Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

  • Of Interest
    · The Elements of Intranet Style
    · A Tutorial in VBScript