VBScript variable

From StealthBot Wiki Backup
Jump to: navigation, search

Variables, like in math, represent a value. In VBScript, they are used to store values and retrieve them later.

Description

A variable is like a box, it can store nearly anything that you tell it to.

Identifiers

First you must give it a name. The name should be informative and tell future readers (including you) what the "box" was holding. The name can contain A through Z, 0 through 9, and/or _ but cannot start with 0 through 9. VBScript ignores case differences, so VAR is the same variable as var. The variable name requirements here apply to all variables, functions, and classes in VBScript. This name is called an "identifier", because it identifies the variable, function, or class.

Storage

Store 1 into Var1:

Var1 = 1

In this example, the variable named Var1 has been given the value of 1.

The = operator is the first operator we will discuss. It will store the value on the right into the variable on the left.

Retrieval

Later in the same code, one can put:

AddQ Var1

This will take the value of Var1, and output it through SSC.AddQ. The bot will write 1 to the channel.

Variable declaration

To declare a variable, use the Dim statement:

Dim Var1

You can declare multiple variables on one line:

Dim Var1, Var3

Explicit

VBScript will create variables when first used if they do not get declared.

The Option Explicit statement may be put at the top of your script to make VBScript require variables to be declared. It has to be the first line of code, but it does not have to be before any comments or white space. If Option Explicit is enabled and a variable is undefined due to a typo or mistake, an error will occur, which can be quite helpful.

Scope

As discussed later, variables declared inside functions and subroutines and classes are in their "local" scope, meaning they (and their value) exist only while the code is executing in the function. When a function is called again, local variables start in their original undeclared state.

Outside of functions, variables are declared in the "global" scope and will keep their value while the script is executing (reset on script reloads).

Privacy

When declaring a variable in the global scope, it is preferred to use either the Public or Private keyword. A Public global variable can be accessed by the declaring script and any other script using the Scripts object. A Private global variable can only be accessed by the declaring script. If you use Dim in the global scope, the variable is public. You cannot declare variables locally using Public or Private.

Constants

A constant is a variable that you declare with its value that cannot be changed.

The format is the Const keyword followed by the variable name followed by the assignment operator (= sign) followed by a literal value. Examples:

Const TheLetterX = "X"
Const UnixTimeStart = #1/1/1970#

You cannot later assign a new value to a constant, but you can retrieve its value just as you would any other variable.

You can use the Public or Private keyword before a global constant to define privacy. You cannot use the Dim keyword with a constant. The only object you are able to store in a constant is Nothing, but any other object comes as the result of a function.

You cannot store other variables or the result of any functions or operations in constants.

Some scripts place constants at the top of the script to globally define settings for a script. When you reload script with the constant set to a different value, that value will be constant for the duration of the script. Effectively, those scripts have a configurable setting that can change once per reload. It is recommended to use the script settings for all script settings you wish to store, so that a user does not have to edit the script and risk script errors.

It is recommended instead to use constants to store literal values that have the possibility of changing, but are not meant as configurable settings.

Literals

A literal is the term for a value that can be put into a variable which is not the result of a function, or another variable.

1 is a numeric literal. Another type of literal, where you see plain text enclosed in "s (quotations) is called a string literal.

Print a string to AddQ.

Var2 = "this is a string"
AddQ Var2

Variable type

VBScript is a loosely typed language, meaning a variable can be assigned any type and later assigned a different value of a different type, without issue. But, if they don't make sense to be compared such as checking if "a" > 1, then an error will be raised ("Type mismatch"). If they do make enough sense for the parser, weird things can be computed (True + 1 results in 0), which is why the language is considered loosely typed, and knowing what your using each variable for is important.

These are the types of VBScript.

Type Result of VarType() Example Literal How to Convert To Description
Empty vbEmpty (0) Empty Indicates Empty (uninitialized)
Null vbNull (1) Null Indicates Null (no valid data, this is not commonly seen)
Integer vbInteger (2) 201 CInt(Value) Indicates an integer (whole number)
Long vbLong (3) 100000 CLng(Value) Indicates a long integer
Single vbSingle (4) CSng(Value) Indicates a single-precision floating-point number (this is not commonly seen)
Double vbDouble (5) 3.14 CDbl(Value) Indicates a double-precision floating-point number (decimal number)
Currency vbCurrency (6) CCur(Value) Indicates a currency (this is not commonly seen)
Date vbDate (7) #1/1/2009# CDate(Value) Indicates a date
String vbString (8) "This is a string." CStr(Value) Indicates a string
Object vbObject (9) Nothing Indicates an automation object
Error vbError (10) Indicates an error (this is never seen)
Boolean vbBoolean (11) True or False CBool(Value) Indicates a Boolean (True or False)
Variant vbVariant (12) Indicates a variant (used only with arrays of Variants)
Data Object vbDataObject (13) Indicates a data-access object (this is not commonly seen)
Decimal vbDecimal (14) C_Dec Value Indicates a decimal value (returned from Visual Basic 6, otherwise never seen)
Byte vbByte (17) CByte(Value) Indicates a byte (this is not commonly seen)
Array vbArray (8192) Indicates an array

Numbers

A number is a value such as 1 or 204.5. In VBScript, there are several types of numbers and several ways to express them.

Integers are whole numbers holding up to a maximum value of 2^15 and minimum value of -2^15. Longs are whole numbers holding up to a maximum value of 2^31 and a minimum value of -2^31. These are often used if it is known that the values are never going to be decimals.

Singles and doubles are "floating point" (decimal) numbers. A double can hold more precise information, is much more commonly used in VBScript, and is the result of arithmetic operations. A double can also hold whole number information, and does so for numbers larger than what a long can hold by storing it with exponent information.

A currency number is a simple type that stores up to four decimal places, useful for monetary values.

Numbers can be represented in several different ways (literals):

  • Positive and negative whole numbers. Ex: 14, -3432 Type: Integer, Long, Double
  • Positive and negative decimal numbers. Ex: 1.434, -0.1 Type: Double
  • Exponential notation where the "E" stands for "10^". Ex: 1E40 (10^40), 2E+1 (20), -78.1E-5 (-.000781) Type: Double.
  • Hexadecimal value, prefixed with "&H". Ex: &H100 (256), &HFFFF (-1), &HFFFF00 (color vbCyan or 16776960) Type: Integer, Long
  • Octal value, prefixed with "&O". Ex: &O100 (64), &O777 (511) Type: Integer, Long

Booleans

A Boolean value is one that is always either True ("on") or False ("off").

Strings

A string is a value containing any text. It is made up of characters. A character is any single letter, number, space, or other symbol you can type on your keyboard (and others that you cannot). The built-in Chr() function results in a single character which corresponds to the provided number.

Strings can be represented by enclosing text in quotation marks ("):

AddQ "This is a string."

To have a quotation mark in a string literal, place two quotation marks next to each other:

AddQ "He said ""Hi"" to me."

String values can be combined with the & operator to combine literals and values resulting from variables or functions.

AddQ "Your username is " & Username & "."

String literals cannot go onto new lines (use the vbNewLine constant below to put a new line into a string value).

The following built-in constants provide string values:

  • vbNullString is an empty string. This is the same as passing "", but is preferred for efficiency.
  • vbNullChar is the null character (also the result of Chr(0)).
  • vbTab is the tab character (result of Chr(9)).
  • vbNewLine is the new line string for the current system. On Windows systems (almost all VBScript systems), this is two characters: "carriage return" followed by "line feed".
  • vbCr is the "carriage return" character (result of Chr(13)).
  • vbLf is the "line feed" character (result of Chr(10)).
  • vbCrLf is the Windows new line string (result of vbCr & vbLf).

Dates

A date is a value containing a date and time value from January 1st, year 100 to December 31st, year 9999. This value often has to be computed and manipulated using the built-in functions.

Date type values can hold time information, date information, or both.

Dates can be represented by enclosing a date-like value in hash marks (#):

VarDate = #January 1, 2009# ' 01/01/2009 00:00:00

The VBScript parser is very good at determining values in date literals. All of the following will work:

VarDate = #1/1/2009 1:01:00 PM# ' 01/01/2009 13:01:00
VarDate = #3 PM# ' <current date> 15:00:00
VarDate = #6.00# ' <current date> 06:00:00
VarDate = #12/1999# ' 12/01/2009 00:00:00
VarDate = #Dec 3# ' 12/03/<current year> 00:00:00

Arrays

Template:Main An array is a special variable containing a set of variables, where each index is like a variable. In VBScript the contents of each "box" can be similar (for example, all Integers) or all completely different (for example, one String followed by an Object).

Objects

Template:Main An object is a special value that can be put in a variable. Objects have properties and functions that can only be accessed by using the special . operator.

See also