Passing empty array to a method

  1. 10 months ago

    Tim S

    5 Sep 2019 Testers 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

    5 Sep 2019 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

    5 Sep 2019 Testers Canterbury, UK
    Edited 10 months 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

    5 Sep 2019 Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    I think Xojo allows optional parameter in middle.

  5. Julian S

    5 Sep 2019 Xojo Pro Answer UK
    Edited 10 months 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

    5 Sep 2019 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

    5 Sep 2019 Testers, Xojo Pro, XDC Speakers, MVP 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

    5 Sep 2019 Xojo Inc scout.galaxy.barn

    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

    5 Sep 2019 Testers, Xojo Pro, XDC Speakers, MVP 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

    5 Sep 2019 Testers 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!