Download presentation
Presentation is loading. Please wait.
1
Chapter 6 Variables 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 Subtype Description Null
Variant intentionally contains no valid data. Boolean Contains either True or False. Byte Contains integer in the range 0 to 255. Integer Contains integer in the range -32,768 to 32,767. Currency -922,337,203,685, to 922,337,203,685, Long Contains integer in the range -2,147,483,648 to 2,147,483,647.
4
Data types in vbscript Single
Contains a single-precision, floating-point number in the range E38 to E-45 for negative values; E-45 to E38 for positive values. Double Contains a double-precision, floating-point number in the range E308 to E-324 for negative values; E-324 to E308 for positive values. Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999. String Contains a variable-length string that can be up to approximately 2 billion characters in length. Object Contains an object. Error Contains an error number.
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 Option Explicit ' Force explicit variable declaration.
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 = 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 Operator Description + Addition - Subtraction *
Multiplication / Division \ Integer Division (divides two numbers and returns an integer result) Mod Modulus (remainder of a division) ^ Exponentiation (raises the number to the power of an exponent)
10
Comparison Operators Operator Description = Is equal to <>
Is not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.