Passing empty array to a method

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.

why not make it an optional parameter?

The array would then be nil.

Possibility but then that parameter would have to be the last one - kind of messes up the “natural” order in this case.

Hmmm - I could make that and subsequent ones optional.

I think Xojo allows optional parameter in middle.

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.

myMethod(a, b, c, Nil, d, e, f)

[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>

As has been alluded, you don’t have to make it optional, you can pass nil and have your code test for that.

You could also overload the method with a signature that doesn’t take an array which just calls the one that does.

The only problem with overloading methods is that the IDE doesn’t help you. It will show the params from one method, but not all.

This is just an aside though.

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.