Redim an array

  1. 2 weeks ago

    I've a global array: Check(), I pass its content to a local array: dummy(),
    I redim the global array Check then..... the local array is also redeemed
    Is it normal?

    Sub Action() Handles Action
      check.Append "any"
      check.Append "name"
      check.Append "could"
      check.Append "be there"
      
      dim dummy() as String = check()
      
      break // dummy is full
      
      redim check(-1)
      
      break // dummy is empty
    End Sub
  2. Kevin G

    Jul 4 Pre-Release Testers, Xojo Pro Gatesheed, England

    Yes. Think of an array as an object. If you want a local copy you will need to clone it by looping round and copying the elements.

  3. Robin L

    Jul 5 Xojo Inc Europe (Germany, Rehlingen)

    See <http://docs.xojo.com/UserGuide:Collections_of_Data> and the section on 'Array Assignment'.
    When viewing the Debugger note that the address for check and dummy is the same.
    The address is the number in square brackets and looks something like [&h<8 digit hex number>]

  4. Edited 2 weeks ago

    Thanks all, I just wonder if this was normal, I used the following solution:

    Sub Action() Handles Action
      check.Append "any"
      check.Append "name"
      check.Append "could"
      check.Append "be there"
      
      dim s as String = join(check,",")
      dim dummy() as String = s.Split(",")
      
      break 
      
      redim check(-1)
      
      break  // now it works
    End Sub
  5. Kem T

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

    I'd expect that to be significantly slower than just looping, but haven't tested.

    dim dummy() as string
    redim dummy( check.Ubound )
    for index as integer = 0 to check.Ubound
      dummy( index ) = check( index )
    next
  6. These arrays have no more than 50 values so I think it won't be significant difference. Either way I read in the forum (i do not remember exactly where) that split was much more efficient.... or maybe I'm confused.

  7. Kem T

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

    It is a more efficient way of working with parts of strings, and it's a more efficient way to do string concatenation.

    The code you posted will certainly work, and is easier to write and read, but it's not the "right" way to do it. If any of your elements contain your join character, for example, it will break. And you can't apply the same technique to any other type of array.

    (Please take this in the spirit it's intended, just as a sharing of experience.)

  8. Edited 2 weeks ago

    Thanks Kem, you're right and probably I'll change it to be more consistent. Although in that case there is no a problem with the join character because the strings are representing codes that are all the same length and contains only a-z/0-9.

    (Please take this in the spirit it's intended, just as a sharing of experience.)

    Sure, and I am grateful you to spend your time to help me.

  9. Norman P

    Jul 5 Pre-Release Testers, Xojo Pro www.great-white-software.com/b...

    @Enric H I've a global array: Check(), I pass its content to a local array: dummy(),

    https://blog.xojo.com/2019/01/23/byref-vs-reference-types/
    https://blog.xojo.com/2019/01/24/some-follow-up-regarding-byref/

or Sign Up to reply!