Passing empty array to a method

  1. 2 weeks ago

    Tim S

    Sep 5 Canterbury, UK

    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:

    Dim empty() as string
    mymethod (empty)

    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.

    myMethod(a, b, c, Nil, d, e, f)
  2. Christian S

    Sep 5 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    why not make it an optional parameter?

    The array would then be nil.

  3. Tim S

    Sep 5 Canterbury, UK
    Edited 2 weeks ago

    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.

  4. Christian S

    Sep 5 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    I think Xojo allows optional parameter in middle.

  5. Julian S

    Sep 5 Pre-Release Testers, Xojo Pro Answer UK
    Edited 2 weeks ago

    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)
  6. Kevin G

    Sep 5 Pre-Release Testers, Xojo Pro Gatesheed, England

    @Tim S 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.

    I logged an enhancement for this a few months back:
    Feedback Case #54740

  7. Kem T

    Sep 5 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

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

  8. Greg O

    Sep 5 Xojo Inc

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

  9. Kem T

    Sep 5 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  10. Tim S

    Sep 5 Canterbury, UK

    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.

or Sign Up to reply!