I have a method one of whose arguments is an array. The method does some stuff with the array, but sometimes I want to have that step skipped in the method. So far, I’ve just been DIMing an empty array and passing that through, but it seems a bit clumsy to have to do:
[quote]Dim empty() as string
mymethod (empty)
[/quote]
Is there a better way to do that so that mymethod will see the passed argument as an empty array (i.e. so that its Ubound is zero)? That way existing code just works.
If you don’t want to refactor your code for Optional at the end or Nil checking, put this at the top of your Method
If arrayProperty = Nil Then
Dim tmpEmptyArray() As String
arrayProperty = tmpEmptyArray
End If
You can then call your Method with Nil in place of the array and it will “fix” it for the code have already written that expects an actual array in the Method.
[quote=452861:@Tim Streater]I have a method one of whose arguments is an array. The method does some stuff with the array, but sometimes I want to have that step skipped in the method. So far, I’ve just been DIMing an empty array and passing that through, but it seems a bit clumsy to have to do:
Is there a better way to do that so that mymethod will see the passed argument as an empty array (i.e. so that its Ubound is zero)? That way existing code just works.[/quote]
I logged an enhancement for this a few months back:
<https://xojo.com/issue/54740>
I am happy with Julian’s method. I’d wondered about passing Nil, but had a feeling the IDE didn’t like that (thought I’d tried it once before). Anyhow I’ve wired that in and changed the couple of calls to use NIL and it seems to be fine. I don’t mind the little bit of extra code in the method as that documents both ends, so to speak.