How do you convert an array of variant into an array of string?

dim keys(-1) as Variant dim skeys(-1) as string keys = dic.keys for each key as string in keys skeys.append key next MyListBox.AddRow(skeys)
To me this seems like a lot of work just to add list of strings to a listbox.

does this not work?

for each key as string in dic.keys
  MyListBox.AddRow(key)
next

Hi Dave!
Nope… that would add a row for every column. :slight_smile:
Addrow can take an array of strings and will put them all in a single row of the listbox with each element in its own column. :slight_smile:
Sadly you can’t pass an array of variants and so you have to convert the variant array into a string array. :frowning:

the keys of a dictionary could be objects, colors, strings, numbers…
Xojo would have a hard time converting those into strings, so that is probably why there is no ‘easy cast’ from an array of variants

Getting strings from keys is in the documentation here
So maybe:

For i As Integer = 0 To MyDict.Count - 1 MyListBox.AddRow(myDict.Key(i).StringValue) Next
Maybe this would work too:

For Each key As Variant In Dic.Keys MyListBox.AddRow(key.stringvalue) Next

[quote=382279:@Brian O’Brien]Hi Dave!
Nope… that would add a row for every column. :slight_smile:
Addrow can take an array of strings and will put them all in a single row of the listbox with each element in its own column. :slight_smile:
Sadly you can’t pass an array of variants and so you have to convert the variant array into a string array. :([/quote]

Your problem is a tad more complex because you have multi columns. Did you code the key names so it becomes possible to affect each to the proper row/column ?

This is a transpose situation

The dictionary is like this:

Apple
Banana
Cherry

Brian appears to want that to end up as a single row in a list box that looks like this:

Apple Banana Cherry

[quote=382312:@Jeff Tullin]This is a transpose situation

The dictionary is like this:

Apple
Banana
Cherry

Brian appears to want that to end up as a single row in a list box that looks like this:

Apple Banana Cherry[/quote]

In fact, the microscopic piece of code he posted does not permit to infer the actual number of columns, and since for each does not guarantee order, one cannot rely on it to assign key content to particular cells.

You can’t cast an array of variants to an array of strings without iteration.
You can’t pass a ParamArray of variants to ListBox.AddRow, it only takes strings.

So I think you’re stuck with what “seems like a lot of work” :slight_smile:

[quote=382279:@Brian O’Brien]Hi Dave!
Nope… that would add a row for every column. :slight_smile:
Addrow can take an array of strings and will put them all in a single row of the listbox with each element in its own column. :slight_smile:
Sadly you can’t pass an array of variants and so you have to convert the variant array into a string array. :([/quote]

This is not exactly an array of variants, but a dictionary.

keys() is an array of variants

If you say so… A one dimension array, yes. The OP shoved a multidimensional array into his dictionary and expect by some magical means array keys to fall in the right cells of a listbox.

He does not even seem to realize that with tab separated data, he could put each key in a listbox as a row as he wishes.

At any rate, his post is now 2 days old, and he seems to have lost interest. Case closed.

Where do you get that? The OP simply pulled the Keys() array from a dictionary and tried to pass that directly to a single listbox row.

He did indeed try to pass keys directly into the listbox row, but he was obviously expecting that somehow the keys would automagically fall on each column…

Once again, since he did not find necessary to participate in the discussion for three days, either he found a solution, gave up or lost interest. No need to drag the corpse any longer…

From where this array of variant comes ?

dictionary.keys

That’s right, how did he fill dictionary.keys ?

It really doesn’t matter.

Because that’s what Listbox.Addrow does?

But you’re right. The OP asked a simple question and got a simple answer. This one is done.

At the condition to have the proper tab separators in the data. But indeed, this one is closed.

It matters. Insteadof populating the dictionary, why not populating the Listbox ?

Also, in the LR, there are three ways to use .AddRow. Choose the one that fit your needs ?