BrainDump
  Home arrow BrainDump arrow Page 2 - Understanding Variables in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Understanding Variables in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-03-18

    Table of Contents:
  • Understanding Variables in VBScript
  • Variable Names and Scope
  • Initializing and Debugging Variables
  • Data types

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Understanding Variables in VBScript - Variable Names and Scope


    (Page 2 of 4 )

    The naming conventions in VBScript are pretty straightforward.  Variable names may not be longer than 256 characters and may not contain spaces.  They must consist only of alphanumeric characters and underscores.  The first character in a variable name cannot be a number.

    Unlike JavaScript, variable names in VBScript are case insensitive.  Their name should be representative of the data that they contain.  For instance, if you have a variable that represents a client’s name, you would be better off naming it "clientname," rather than something obscure like "n1."  It’s also good practice to use the recommended naming conventions, although it is not required by the interpreter.

    According to the naming conventions, a variable name should begin with an abbreviation that represents the intended data type for its value.  Parts of the name should also be capitalized for readability.  Following the naming conventions, the variable in the last example should be named "strClientName," since it most likely would reference a string containing a client’s name.  The table below lists the data subtypes in VBScript along with their abbreviations.

    Subtype

    Abbr

    Example

    Boolean

    bln

    blnFound

    Byte

    byt

    bytRasterData

    Date (Time)

    dtm

    dtmStart

    Double

    dbl

    dblTolerance

    Error

    err

    errOrderNum

    Integer

    int

    intQuantity

    Long

    lng

    lngDistance

    Object

    obj

    objCurrent

    Single

    sng

    sngAverage

    String

    str

    strFirstName

    While this isn’t part of the official naming conventions, another common practice is to name object variables with prefixes that are indicative of the object’s type.  The table below shows a list of common prefixes that I often use in my code examples.

    Object Type

    Abbr

    Example

    Array

    arr

    arrNames

    Collection

    col

    colItems

    Dictionary

    dict

    dictStateAbbr

    External Object

    obj

    objFso

    According to the naming conventions, variables should also be named according to their scope.  A variable’s scope is the area within a script or program from which the variable may be safely referenced.  This is determined by where the variable is initialized.

    A variable’s scope may be either procedure-level or script-level (sometimes referred to as global).  A procedure level variable is initialized within a procedure, such as a function, subroutine, or event.  Script-level variables may be initialized anywhere outside of a procedure.

    Attempting to reference a variable outside of its scope will result in a runtime error.

    The naming conventions suggest adding an "s" prefix to any script-level variable.  Procedure-level variables do not change.  I’ve provided a simple example below.

    sstrScriptLevel = "A script level variable."

     

    'The following line results in a runtime error because the variable

    'is out of scope.

    WScript.Echo strProcedureLevel

     

    Sub MySub

       strProcedureLevel = "A procedure level variable."

      

       ' The following line is completely valid.

       WScript.Echo sstrScriptLevel

    End Sub

    By now you may be wondering why you should bother with the naming conventions at all, if they’re not required by the interpreter.  The naming conventions were developed as a way to help multiple programmers working on the same project, or to help a single developer with a large project.

    By using naming conventions, you can tell at a glance what data type a variable will provide and what its scope is.  This is especially useful if you are reworking someone else’s code or updating code you may have written in the past.

    More BrainDump Articles
    More By Nilpo


       · If you plan to master any programming language, it's a good practice to develop a...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT