VB 变量

Dim、Variant、命名规则、数组、常量与作用域。

Beginner
Key PointsVBScript 只有 Variant 一种类型,用 Dim 声明;数组用 Dim a(n),n 是最大下标不是长度。

声明

VBSCRIPT
Dim name
Dim age, city
name = "Ada"
age = 36
city = "北京"

WScript.Echo name & " / " & age & " / " & city
Output
Ada / 36 / 北京

Dim a, b, c declares several at once. Names are case-insensitive, so Name and name are the same variable — which is why typos are hard to spot. Always start with Option Explicit.

Option Explicit

VBSCRIPT
Option Explicit
Dim total
totl = 10            ' 会报错:变量未定义
Output
Microsoft VBScript runtime error: Variable is undefined: 'totl'

Without Option Explicit, totl = 10 silently creates a new variable and total stays empty — the result comes out 0 while the code looks correct.

Variant 的子类型

VBSCRIPT
Dim v

v = "10"      : WScript.Echo TypeName(v)     ' String
v = 10        : WScript.Echo TypeName(v)     ' Integer
v = 10.5      : WScript.Echo TypeName(v)     ' Double
v = Now()     : WScript.Echo TypeName(v)     ' Date
v = True      : WScript.Echo TypeName(v)     ' Boolean
v = Empty     : WScript.Echo TypeName(v)     ' Empty
v = Null      : WScript.Echo TypeName(v)     ' Null
Output
String
Integer
Double
Date
Boolean
Empty
Null

Empty means declared but unassigned; Null means explicitly no value. They differ: Empty = 0 is true, while Null = 0 is Null — neither true nor false. Test with IsEmpty() and IsNull().

数组

VBSCRIPT
Dim langs(2)                 ' 下标 0..2,共 3 个元素
langs(0) = "VBScript"
langs(1) = "JScript"
langs(2) = "PowerShell"

WScript.Echo "元素个数:" & (UBound(langs) - LBound(langs) + 1)

Dim i
For i = LBound(langs) To UBound(langs)
    WScript.Echo i & ": " & langs(i)
Next

' 动态数组
Dim dyn()
ReDim dyn(3)
dyn(0) = "a"
ReDim Preserve dyn(5)        ' 保留已有元素
Output
元素个数:3
0: VBScript
1: JScript
2: PowerShell

Dim a(2) is three elements, not two: the number is the highest index. ReDim clears the data unless you add Preserve, and Preserve only works on the last dimension.

常量

VBSCRIPT
Const PI = 3.14159
Const APP_NAME = "Demo"
Const MAX_RETRY = 3

WScript.Echo APP_NAME & " v1.0, PI=" & PI
Output
Demo v1.0, PI=3.14159

Assigning to a constant again raises an error. ALL-CAPS naming is the convention, signalling "configuration, do not change". VBScript has no enums, so related constants are grouped by prefix (COLOR_RED, COLOR_BLUE).

作用域

VBSCRIPT
Dim g_count
g_count = 0

Sub Increase()
    g_count = g_count + 1        ' 改的是脚本级变量
End Sub

Sub LocalDemo()
    Dim g_count                  ' 遮蔽脚本级变量
    g_count = 99
    WScript.Echo "局部:" & g_count
End Sub

Increase
LocalDemo
WScript.Echo "全局:" & g_count
Output
局部:99
全局:1

Dim-ing the same name inside a procedure shadows the outer variable silently. That is the classic "I changed it but nothing happened" bug, and Option Explicit does not catch it — only naming discipline does.

VBScript has no block scope: a Dim inside an If is visible outside it, and a For loop variable survives the loop. Programmers from C-family languages misjudge this constantly.

Friends & Partners