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

  1. 3 months ago

    Brian O

    Apr 12 Pre-Release Testers Calgary, AB
    Edited 3 months ago
    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.

  2. Dave S

    Apr 12 San Diego, California USA

    does this not work?

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

    Apr 12 Pre-Release Testers Calgary, AB
    Edited 3 months ago

    Hi Dave!
    Nope.. that would add a row for every column. :)
    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. :)
    Sadly you can't pass an array of variants and so you have to convert the variant array into a string array. :(

  4. Jeff T

    Apr 12 Midlands of England, Europe

    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

  5. Eric W

    Apr 12 Pre-Release Testers, Xojo Pro
    Edited 3 months ago

    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
  6. Michel B

    Apr 13 Pre-Release Testers RubberViews.com

    @Brian OBrien Hi Dave!
    Nope.. that would add a row for every column. :)
    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. :)
    Sadly you can't pass an array of variants and so you have to convert the variant array into a string array. :(

    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 ?

  7. Jeff T

    Apr 13 Midlands of England, Europe

    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

  8. Michel B

    Apr 13 Pre-Release Testers RubberViews.com

    @Jeff T 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

    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.

  9. Julia T

    Apr 13 Sandy Hook, Connecticut

    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" :)

  10. Michel B

    Apr 13 Pre-Release Testers RubberViews.com

    @Brian OBrien Hi Dave!
    Nope.. that would add a row for every column. :)
    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. :)
    Sadly you can't pass an array of variants and so you have to convert the variant array into a string array. :(

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

  11. Julia T

    Apr 14 Sandy Hook, Connecticut

    keys() is an array of variants

  12. Michel B

    Apr 14 Pre-Release Testers RubberViews.com
    Edited 3 months ago

    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.

  13. Tim H

    Apr 14 Pre-Release Testers Portland, OR USA

    @Michel B The OP shoved a multidimensional array into his dictionary

    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.

  14. Michel B

    Apr 15 Pre-Release Testers RubberViews.com
    Edited 3 months ago

    @Brian OBrien Nope.. that would add a row for every column. :)

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

  15. Emile S

    Apr 15 Europe (France, Strasbourg)

    From where this array of variant comes ?

  16. Julia T

    Apr 15 Sandy Hook, Connecticut

    dictionary.keys

  17. Emile S

    Apr 15 Europe (France, Strasbourg)

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

  18. Tim H

    Apr 15 Pre-Release Testers Portland, OR USA

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

    It really doesn't matter.

    @Michel B he was obviously expecting that somehow the keys would automagically fall on each column.

    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.

  19. Michel B

    Apr 15 Pre-Release Testers RubberViews.com

    @Tim H Because that's what Listbox.Addrow does?

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

  20. Emile S

    Apr 15 Europe (France, Strasbourg)

    @Tim H It really doesn't matter.

    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 ?

  21. Newer ›

or Sign Up to reply!