VB 变量
Dim、Variant、命名规则、数组、常量与作用域。
声明
Dim name
Dim age, city
name = "Ada"
age = 36
city = "北京"
WScript.Echo name & " / " & age & " / " & cityAda / 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
Option Explicit
Dim total
totl = 10 ' 会报错:变量未定义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 的子类型
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) ' NullString
Integer
Double
Date
Boolean
Empty
NullEmpty 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().
数组
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) ' 保留已有元素元素个数:3
0: VBScript
1: JScript
2: PowerShellDim 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.
常量
Const PI = 3.14159
Const APP_NAME = "Demo"
Const MAX_RETRY = 3
WScript.Echo APP_NAME & " v1.0, PI=" & PIDemo v1.0, PI=3.14159Assigning 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).
作用域
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局部:99
全局:1Dim-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.
Dim inside an If is visible outside it, and a For loop variable survives the loop. Programmers from C-family languages misjudge this constantly.声明
Dim name
Dim age, city
name = "Ada"
age = 36
city = "北京"
WScript.Echo name & " / " & age & " / " & cityAda / 36 / 北京Dim a, b, c 一次声明多个。变量名不区分大小写,Name 与 name 是同一个——这也是为什么拼写错误很难发现,务必在文件开头写 Option Explicit。
Option Explicit
Option Explicit
Dim total
totl = 10 ' 会报错:变量未定义Microsoft VBScript runtime error: Variable is undefined: 'totl'不加 Option Explicit 时,totl = 10 会悄悄新建一个变量,total 仍然是空——最后算出来的结果是 0,而代码看起来完全正常。
Variant 的子类型
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) ' NullString
Integer
Double
Date
Boolean
Empty
NullEmpty 是「声明了但没赋值」,Null 是「明确表示没有值」,两者不同:Empty = 0 为真,Null = 0 为 Null(不是 True 也不是 False)。判断用 IsEmpty() 和 IsNull()。
数组
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) ' 保留已有元素元素个数:3
0: VBScript
1: JScript
2: PowerShellDim a(2) 是 3 个元素,不是 2 个——括号里是最大下标。ReDim 会清空数据,要保留必须 Preserve,且只能改最后一维。
常量
Const PI = 3.14159
Const APP_NAME = "Demo"
Const MAX_RETRY = 3
WScript.Echo APP_NAME & " v1.0, PI=" & PIDemo v1.0, PI=3.14159常量赋值后再改会报错。命名全大写是惯例,一眼能看出「这是配置,不该被改」。VBScript 没有真正的枚举,一组相关常量靠命名前缀区分(COLOR_RED、COLOR_BLUE)。
作用域
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局部:99
全局:1过程内 Dim 同名变量会遮蔽外层,且不报错。这是「明明改了却没生效」的经典来源——Option Explicit 也防不住,只能靠命名区分。
If 里 Dim 的变量在 If 外面照样能访问,For 的循环变量在循环结束后依然存在。写惯了 C 系语言的人很容易误判。