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

    Exiting a Routine with the Exit Statement

    Ordinarily, when you call a function or a subroutine, all code between the initial Function or Sub statement and the concluding End Function or End Sub statement is executed. In some cases, though, you may not want all of a routine's code to be executed.

    For example, imagine a situation in which you only want to execute a subroutine if a particular condition is met. One way of implementing this in your code is to test for the condition before calling the subroutine, as follows:

    . . . some code
    If condition Then
      Call MySubRoutine(  )
    End if
    . . . more code
    

    However, if you call the routine from multiple locations in your code, and you want to apply this test to each call, you'll have to include this control structure at every place in the script in which you call the subroutine. To avoid this redundant code, it's better to call the subroutine regardless of the condition, and to place the test within the subroutine. One way of doing this is as follows:

    Sub MySubRoutine(  )
       If condition then
          . . . all our subroutine code
       End if
    End Sub
    

    This is all well and good, and quite legal. However, in a large and complex subroutine, the End If statement becomes visually lost, especially if there are several conditions to be met. The preferred alternative is the Exit Sub and the Exit Function statements, which are used with the Sub . . . End Sub and Function . . . End Function constructs, respectively. Our conditional test at the beginning of a subroutine then appears as follows if we use the Exit Sub statement:

    Sub MySubRoutine(  )
     If Not condition Then
       Exit Sub
     End if
     . . . all our subroutine code 
    End Sub
    

    Exit Sub and Exit Function immediately pass execution of the program back to the calling procedure; the code after the Exit statement is never executed. As you can see from the previous code fragment, the code is clean and clearly understandable. If the particular condition is not met, the remainder of the subroutine is not executed. Like the Exit Do and Exit For statements, any number of Exit Sub or Exit Function statements can be placed anywhere within a procedure, as the following code fragment demonstrates:

    Function functionname(argumentlist)
     
     . . . some calculation or manipulation
     
       If condition1 Then
         functionname = result of calculation or manipulation
         Exit Function
       End If
     
     . . . perhaps some more code
     
       If condition2 Then
         functionname = result of calculation or manipulation
         Exit Function
       End If
     
    End Function
    

    Class Modules

    Since VBScript 5.0, developers have been able to create classes to use in their scripts--a definite step along the road of object-oriented programming in VBScript. Writing classes with VBScript is very similar to writing COM objects with VB. Before we look at writing an actual class, let's go over some of the terminology so we are clear on what we are doing and what we are referring to.

    A class is simply the template for an object. When you instantiate an object (that is, create an instance of a class) in code, VBScript makes a copy of the class for your use. All objects come from a class. Writing the class is simply a matter of creating a design for the objects that you want to use.

    So naturally, it follows that an object is simply a copy of the class that you are making available to your program. You can make as many copies as you like for your use. The copies are temporary structures for holding information or creating interactions. When you are done with the objects, you can release them. If you need another one, you can instantiate another copy.

    In VBScript, classes must be created in the scripts where you want to use them or they must be included in the scripts that use them. Since VBScript isn't compiled, you don't have the advantage of being able to write a set of VBScript COM classes that are usable outside of the scripts in which they're defined, or that can be easily accessed by programs and scripts written in other languages.

    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
  • 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