ParamArray: Are you really an array?

Misunderstandings about ParamArray.

From the documentation example

Function AddNumbers(ParamArray nums As Integer) As Integer
  ' nums is treated as an array
  Var i, total As Integer
  For Each i In nums
    total = total + I
  Next
  Return Total
End Function

The comment says that nums is treated as an array. But it seems to be actually some other entity.

If it is “treated as an array” why doesn’t

nums.Count autocomplete?

Var howMany As Integer
howMany = nums.Count

This seems to be legal. I assume it is kosher?

But I was put off by the fact that autocompletion offered what one might expect from an Integer: FromBinary, FromHex, ToString etc.

Var sHowMany As String
sHowMany = nums.ToString // which would not seem to make sense

And indeed the compiler will not accept this.

The documentation states “The ParamArray parameter is treated as an array in the method code”, why doesn’t autocomplete reflect this?

ParamArray ends as an Array at end. Count works.

That said, autocomplete is an independent piece of code trying to figure out things and this resolution may be defective.

If you declare nums() instead of nums then autocomplete treats it like an array.

4 Likes

Andrew,

That solves my problem. I think that the documentation should make the situation clear and offer the syntax that you suggest as the default. As long as I can remember this detail, I am OK. I will just include the parentheses when using ParamArray as you suggest.

ParamArray args and ParamArray args() should not be synonyms, the blessed correct way is ParamArray args(). So declaring the unholy form ParamArray args as a compiler error as VB.Net does AFAIK would fix completely the problem. Removing doubts increase safety.

Let’s observe VB.Net for Linux behavior.

First as Array, the correct one:

image

Now let's declare it as some regular type. That's unexpected:

image

Logically I would prefer a more humanized error message as

Syntax error on line 6: 'ParamArray args', did you mean 'ParamArray args()' ?
2 Likes