Can you declare a constant array in VBScript?

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

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

For each quarter q q. Before some work

Etc.

Can this be done in VBScript?

+7
vbscript
source share
5 answers

An array is the result of calling a function ( Array() ) in VBScript. Only literal values ​​can be made by Const . So: No, you cannot.

+10
source share

Why not just declare the array open and then assign the array at the beginning of the script?

 Public myArray(3) arrQuarters = Array("Q1", "Q2", "Q3", "Q4") For Each Quarter in arrQuarters wscript.echo Quarter Next 
+6
source share

You can define a function to return the array that 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 
+3
source share

The simple answer is no. Array cannot be executed const .

+2
source share

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 may 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" 

Attention: never choose a separator that may appear in your "atomic" data rows, or the function will be divided on this separator in the middle of one piece of data.

+1
source share

All Articles