Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Array / Variant confusion (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Sheet Window opens in wrong window?
Next thread: Profile.txt is not a CSV?


Reading *CSV Files versus *.Txt Files   -   Claude Stone
  Array / Variant confusion   -   Michael Diehr
   Re: Array / Variant confusion   -   Thomas Tempelmann
    Re: Array / Variant confusion   -   Michael Diehr
   Re: Array / Variant confusion   -   Joe Huber

Array / Variant confusion
Date: 23.11.08 19:37 (Sun, 23 Nov 2008 10:37:55 -0800)
From: Michael Diehr
I'm trying to emulate an array of arrays, by creating an array of
variants, each of which holds an array. However, I'm running into a
bug (probably in my understanding) of how arrays are stored in variants.

It appears as if they are stored by reference, not by value.

Let's say I want to create an array of arrays as follows:
List(0) = Array(Apple, 1)
List(1) = Array(Banana, 2, Gopher)

The following code doesn't work as expected:

dim list() as variant ' array of arrays
dim items() as string

items.append "apple"
items.append "1"
list.append items
redim items(-1)
items.append "banana"
items.append "2"
items.append "gopher"

list.append items

The flaw seems to be when i say 'redim items(-1)' instead of
creating a new items() array, it just wipes out the old one, a
reference to which is stored in the list() array of variants.

So -- is there a way to do this? Basically I need to do something like
dim items() as NEW string
to create a new array object, rather than re-using the old one.
Arrays are weird -- sometimes they behave like objects, and sometimes
they don't. How do i make this code work?



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Array / Variant confusion
Date: 23.11.08 20:19 (Sun, 23 Nov 2008 20:19:19 +0100)
From: Thomas Tempelmann
On 23.11.2008 19:37 Uhr, "Michael Diehr" <<email address removed>> wrote:

> It appears as if they are stored by reference, not by value.

Right.

And there's no RB function that would create a (shallow) copy of an array
for you, either, which is a bit of a bummer.

You'll have to run your own loop in which you create a new array and then
copy each of the other array's values.


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Array / Variant confusion
Date: 23.11.08 20:39 (Sun, 23 Nov 2008 11:39:20 -0800)
From: Michael Diehr
On Nov 23, 2008, at 11:19 AM, Thomas Tempelmann wrote:

> On 23.11.2008 19:37 Uhr, "Michael Diehr" <<email address removed>> wrote:
>
>> It appears as if they are stored by reference, not by value.
>
> Right.
>
> And there's no RB function that would create a (shallow) copy of an
> array
> for you, either, which is a bit of a bummer.
>
> You'll have to run your own loop in which you create a new array and
> then
> copy each of the other array's values.

thanks -- arrays in realbasic does seem to me somewhat counterintuitive.

Anyway, I solved the issue by creating a new class, cArray which holds
an array of variants, and has the array methods (append, remove,
etc.), and uses Operator_Convert() to convert back to the desired
array type. Since it's a class, it's easy to NEW it to get an actual
new array object.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Array / Variant confusion
Date: 23.11.08 20:34 (Sun, 23 Nov 2008 11:34:23 -0800)
From: Joe Huber
At 10:37 AM -0800 11/23/08, Michael Diehr wrote:
>Let's say I want to create an array of arrays as follows:
> List(0) = Array(Apple, 1)
> List(1) = Array(Banana, 2, Gopher)
>
>The following code doesn't work as expected:
>
>dim list() as variant ' array of arrays
>dim items() as string
>
>items.append "apple"
>items.append "1"
>list.append items
>redim items(-1)
>items.append "banana"
>items.append "2"
>items.append "gopher"
>
>list.append items

Michael

Here are several approaches I'd consider:

- Use the array function as shown in this tested example

//Let's say I want to create an array of arrays as follows:
dim list(1) as variant ' array of arrays
List(0) = Array("Apple", "1")
List(1) = Array("Banana", "2", "Gopher")
dim s1(), s2() as string
s1 = List(0)
s2 = List(1)
dim Fruit1, Fruit2 as string
Fruit1 = s1(0)
Fruit2 = s2(0)

- Use a method to create a new string array, populate and return it

- Use a class to wrap either a few properties or a string array and
then you can easily create new instances normally

Hope this helps,
Joe Huber

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>