3

I get an error:

invalid procedure call or argument: 'AddRange'

when passing a variable to ArrayList.AddRange(), but the code works fine when I pass the return value of a function instead, i.e.

My list:

Dim Foo
Set Foo = CreateObject("System.Collections.ArrayList")

Adding a new list returned by GetList() works fine:

Call Foo.AddRange(GetList()) ' works fine

Function GetList()
  Set GetList = CreateObject("System.Collections.ArrayList")
End Function

but passing a new list with a variable (x) raises an error:

Dim x
Set x = CreateObject("System.Collections.ArrayList")

Call Foo.AddRange(x)  ' error: invalid procedure call or argument: 'AddRange'

What is going on?

Bond
  • 16,071
  • 6
  • 30
  • 53
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137

1 Answers1

4

Looks like you need to pass the array list by value. This works:

Call Foo.AddRange((x))

The inner set of parentheses causes the argument to be passed ByVal instead of ByRef.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • That works, but why? I was thinking that during execution a method `ArrayList.AddRange(object)` was searched for and not found, since there is only a `ArrayList.AddRange(ICollection)`, but given your answer, this doesn't seem to be the right. – Micha Wiedenmann Jul 31 '15 at 16:50
  • I suspect that `AddRange` doesn't accept object references, and that the return value of the function is implicitly passed by value instead of by reference, but I don't know where (or if) this is documented. – Ansgar Wiechers Jul 31 '15 at 17:27
  • Sorry, no doc nor theory, but another instance of this problem: http://stackoverflow.com/a/31455471/603855 – Ekkehard.Horner Jul 31 '15 at 18:14