你能在VBScript中声明一个常量数组吗?

时间:2022-12-06 07:40:13

I want to use an array that I declare once at the top of my code several times. Ex.

我想使用一个我在代码顶部多次声明一次的数组。防爆。

Const Quarters = ["Q1", "Q2", "Q3", "Q4"]

Const Quarters = [“Q1”,“Q2”,“Q3”,“Q4”]

For each Quarter q q.Do some work

对于每个季度q q。做一些工作

Etc.

Can this be done in VBScript?

这可以在VBScript中完成吗?

5 个解决方案

#1


An array is the result of a function call (Array()) in VBScript. Only literal values can be made Const. So: No, you can't.

数组是VBScript中函数调用(Array())的结果。只有文字值可以构成Const。所以:不,你不能。

#2


Why not just declare the array as public and then assign the array during the start of the script?

为什么不将数组声明为public,然后在脚本开始时分配数组?

Public myArray(3) 
arrQuarters = Array("Q1", "Q2", "Q3", "Q4")

For Each Quarter in arrQuarters
    wscript.echo Quarter
Next

#3


You could define a function to return the array you want to use as a constant. For example:

您可以定义一个函数来返回要用作常量的数组。例如:

For Each q in AllQuarters
    wscript.echo q
Next

wscript.echo "element 0 = " & AllQuarters()(0)

AllQuarters()(0) = "X1"

wscript.echo "element 0 still = " & AllQuarters()(0)


Function AllQuarters()
    AllQuarters = Array("Q1","Q2","Q3","Q4")
End Function

#4


Simple answer: no. The array cannot be made const.

简单回答:没有。该数组不能成为const。

#5


A shorter and less error-prone solution would be:

更短且不易出错的解决方案是:

Dim arr
arr = Split("Q1 Q2 Q3 Q4") : ubd = UBound(arr)
' Implied separator is " " aka 040 octal aka 32 Dec aka 020 Hex.

If your data might contain spaces:

如果您的数据可能包含空格:

arr = Split("Le Sage,ne pleure,ni les vivants, ni les morts", ",")
ubd = UBound(arr)
' arr(2), for instance, now contains "ni les vivants"

Caution: Never choose a separator that might occur in your 'atomic' data strings, or the function will split on that separator in the middle of a single piece of data.

警告:永远不要选择“原子”数据字符串中可能出现的分隔符,否则该函数将在单个数据片段的中间拆分该分隔符。

#1


An array is the result of a function call (Array()) in VBScript. Only literal values can be made Const. So: No, you can't.

数组是VBScript中函数调用(Array())的结果。只有文字值可以构成Const。所以:不,你不能。

#2


Why not just declare the array as public and then assign the array during the start of the script?

为什么不将数组声明为public,然后在脚本开始时分配数组?

Public myArray(3) 
arrQuarters = Array("Q1", "Q2", "Q3", "Q4")

For Each Quarter in arrQuarters
    wscript.echo Quarter
Next

#3


You could define a function to return the array you want to use as a constant. For example:

您可以定义一个函数来返回要用作常量的数组。例如:

For Each q in AllQuarters
    wscript.echo q
Next

wscript.echo "element 0 = " & AllQuarters()(0)

AllQuarters()(0) = "X1"

wscript.echo "element 0 still = " & AllQuarters()(0)


Function AllQuarters()
    AllQuarters = Array("Q1","Q2","Q3","Q4")
End Function

#4


Simple answer: no. The array cannot be made const.

简单回答:没有。该数组不能成为const。

#5


A shorter and less error-prone solution would be:

更短且不易出错的解决方案是:

Dim arr
arr = Split("Q1 Q2 Q3 Q4") : ubd = UBound(arr)
' Implied separator is " " aka 040 octal aka 32 Dec aka 020 Hex.

If your data might contain spaces:

如果您的数据可能包含空格:

arr = Split("Le Sage,ne pleure,ni les vivants, ni les morts", ",")
ubd = UBound(arr)
' arr(2), for instance, now contains "ni les vivants"

Caution: Never choose a separator that might occur in your 'atomic' data strings, or the function will split on that separator in the middle of a single piece of data.

警告:永远不要选择“原子”数据字符串中可能出现的分隔符,否则该函数将在单个数据片段的中间拆分该分隔符。