1 / 13

Mastering Variables in VBScript

Understand variables in VBScript, including data types, declaration methods, naming rules, scopes, and using Constants. Learn about creating and working with variables for efficient scripting.

cmorse
Download Presentation

Mastering Variables in VBScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6Variables What is VBScript? • VBScript is a scripting language • A scripting language is a lightweight programming language • VBScript is a light version of Microsoft's programming language Visual Basic How Does it Work? When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. For those of you who have not used variables before, then you should know that the use of variables helps to make programs easier to program and to understand. Variables are used to hold information so that it can be reused throughout the program. Creating variables in vbscript as follows. Dim Myvar Dim total

  2. Data types in vbscript. • There are many different data types you might want to be able to store into variable: numbers, words, dates and many more. • VBScript supports the following data types 1) Integer 2) Float 3) String 4) Date 5) Boolean 6) Currency 7) Object 8) Variant

  3. Data types in vbscript

  4. Data types in vbscript

  5. Declaring variables • There are two methods for declaring variables in VBScript, explicitly and implicitly. You usually declare variables explicitly with the Dim statement: Dim Name • This statement declares the variable Name. You can also declare multiple variables on one line as shown below, although it is preferable to declare each variable separately: Dim Name, Address, City, State • Variables can be declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug.

  6. Declaring variables Variable Naming Rules When naming variables the following rules apply: • They must begin with an alphabetic character • They cannot contain embedded periods • They must be unique within the same scope. • They must be no longer than 255 characters

  7. Declaring variables • Scope of Variables • The scope of a variable dictates where it can be used in your script. A variable's scope is determined by where it is declared. If it is declared within a procedure, it is referred to as a procedure-level variable and can only be used within that procedure. If it is declared outside of any procedure, it is a script-level variable and can be used throughout the script. • The example below demonstrates both script-level and procedure-level variables. <SCRIPT>   Dim counter   Sub Abc1     Dim temp   End Sub </SCRIPT> The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the Abc1 sub-procedure.

  8. Option Explicit • The Option Explicit statement forces the explicit declaration of all variables using the Dim, Private, Public, or ReDim statements. In a long program, this statement prevents the accidental reuse of the name of a previously declared variable. Also, if you mistype a declared variable's name or try to use an undeclared variable, an error message is generated. Note that the Option Explicit statement must be placed at the top of the code before any other VBScript commands or any HTML code. Code: <% Option Explicit %> < HTML > < HEAD > < TITLE > EXAMPLE < /TITLE > < /HEAD > < BODY > <%  Dim myvariable,yourvar   myvariable = 123.456   yourvar = 0%> < /BODY > < /HTML > • Option Explicit ' Force explicit variable declaration. • Dim MyVar ' Declare variable. • MyInt = 10 ' Undeclared variable generates error. • MyVar = 10 ' Declared variable does not generate error.

  9. Arithmetic Operators

  10. Comparison Operators

  11. Constants • What Is a Constant? • A constant is a meaningful name that takes the place of a number or string and never changes. VBScript defines a number of intrinsic constants. You can get information about these intrinsic constants from the VBScript Language Reference. Creating Constants • You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values. For example: Const MyString = "This is my string." Const MyAge = 49 Note that the string literal is enclosed in quotation marks (" "). Quotation marks are the most obvious way to differentiate string values from numeric values. Date literals and time literals are represented by enclosing them in number signs (#). For example: Const CutoffDate = #6-1-97# You may want to adopt a naming scheme to differentiate constants from variables. This will prevent you from trying to reassign constant values while your script is running.

  12. String Manipulation These functions allow you to manipulate string data. • UCase(string) returns string with all lowercase letters converted to uppercase letters. • LCase(string) returns string with all uppercase letters converted to lowercase letters. • LTrim(string) removes all the spaces from the left side of string. • RTrim(string) removes all the spaces from the right side of string. • Trim(string) removes spaces from both the left and the right sides of string. • Len(string) returns the number of characters in string. • Len(variable) returns the number of bytes required by variable. • LenB(string) returns the number of bytes required to store string. • StrReverse(string) returns string with the characters in reverse order.

  13. Arrays • Arrays are the list of similar data items, which are stored in unique variable. • Arrays may declared in two ways. • Dim a • a= Array( 1, 2,3 ….) Or Dim Myarray (3) Myarray(0)=1 Myarray(1)=2 . .so on.

More Related