此代码
07: ReDim newArray(0)
08: ReDim oldArray(0)
'populating newArray
40: wscript.echo "redimming oldArray to length of " & UBound(newArray)
41: ReDim oldArray(UBound(newArray))
42: wscript.echo UBound(oldArray)
43: oldArray = newArray
产生以下输出:
redimming oldArray to length of 19
19
D:\Scripts\test.vbs(43, 2) Microsoft VBScript runtime error: Type mismatch
如何制作“newArray()”的副本? (Created via this question)
答案 0 :(得分:-1)
在VBScript中,数组赋值复制右侧值:
>> Dim a : a = Array(1,2,3)
>> Dim b : b = a
>> b(0) = 17
>> WScript.Echo Join(a), Join(b)
>>
1 2 3 17 2 3
(与其他语言相反,数组赋值通常意味着为r值的别名提供/。)
所以:不要为l值创建一个数组,只需将名称变暗(允许Option Explicit
)。
尝试II:
如果需要动态数组的副本,只需将其分配给一个干净/新变量。
BTW:ReDim newArray(0)
- 创建一个包含一个(空)元素的数组;反思榆树大小/数量与UBound / Last Index之间的差异。
尝试III:
ReDim a(n) ' create array for/with n+1 elements
... fill a ...
Dim b ' plain variant variable
b = a ' copy a (in)to b