Redim an array

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?

[code]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
[/code]

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.

See http://documentation.xojo.com/getting_started/using_the_xojo_language/collections_of_data.html 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>]

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

[code]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
[/code]

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

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.

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.)

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.

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

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

How to redim the data values in the this following code:

dim a(3), b(3), ab(3) as double  
dim Nb as integer
Nb=2
a(1) = 1
b(1)= 2
a(2)= 3
b(2)= 4

For i=1 to Nb
ab(i)= a(i)*b(i)
Next

Is it possible to declare a(),b(),ab() , depending of Nb with redim ?

You do not found the answer there: http://documentation.xojo.com/api/language/redim.html ?

dim a(-1), b(-1), ab(-1) as double  
dim Nb as integer
Nb=2
redim a(nb)
redim b(nb)
redim ab(nb)

Emile , I have seen already in he xojo doc.
I don’t know the value of Nb, thus a() can’t be defined , i do a(999) this is static value , is it the best way ?
Dynamical resize is better.