1

I'm a beginner in vbscript. I defined an array like

Dim MyArray(5)
MyArray(0) = "foo"
MyArray(1) = "bar"
MyArray(2) = "foobar"
MyArray(3) = "foo"
MyArray(4) = "barbar"
MyArray(5) = "foofoo"

I wondered if there is a syntax like in other languages e.g. PHP like

MyArray = Array("foo", "bar", "foobar", "foo", "barbar", "foofoo")

1 Answer 1

3

Yes:

>> MyArray = Array("foo", "bar", "foobar", "foo", "barbar", "foofoo")
>> WScript.Echo Join(MyArray, "-")
>>
foo-bar-foobar-foo-barbar-foofoo
>>

But: Dim MyArray(5) declares/defines a fixed/non-resizeable array while the Array function returns an array that can grow/shrink via ReDim Preserve.

Update wrt comment:

You should Dim all your variables (and enforce this rule by using Option Explicit). So my code snippet should have been:

>> Dim MyArray : MyArray = Array("foo", "bar", "foobar", "foo", "barbar", "foofoo")
>> WScript.Echo Join(MyArray, "-")
>>
foo-bar-foobar-foo-barbar-foofoo
>>

VBScript is weakly typed. In general, a variable will get its (sub)type from the assigned value; if you assign the return value of functions returning an array (like Array() or Split()) to a variable it will 'accept' it. Fixed arrays, however, are an exception to this general rule.

Sign up to request clarification or add additional context in comments.

1 Comment

Thst means that I should rather define with Dim MyArray ? Will this variable accept an array ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.