variant array to string array

I want to transfer the values from a dictionary to an array. The dictionary values are strings, but the dictionary.values method returns a variant array. Looking through the documentation, there doesn’t appear to be any way of converting directly from one to the other. So it appears necessary to use an intermediate array and a loop. This is what I’m doing:

  dim s() as String
  dim temp() as variant = myDict.Values
  ReDim s(UBound(temp))
  for i=0 to UBound(temp)

Is there an easier way to do this rather than using a loop?

Assuming that I have to use a loop, would the following, using append rather than redim, be more or less efficient than the above loop? There will be roughly 2000 array elements.

  dim s() as String
  dim temp() as variant = myDict.Values
  for i=0 to UBound(temp)

Append will be slightly less efficient than redim. For only 2000 elements it may not matter.

You have to use a loop. The only casting of arrays allowed is upcasting Object/Interface arrays.

What you can do though is wrap your loop in an extension method

Function AsStringArray( extends va() As Variant) As String() // End Function

then you can write

dim s() as String = myDict.Values.AsStringArray

Thanks. I’ll probably just leave it as inline code, since I only have to do it once.

Thanks. That’s what I suspected, especially for arrays with fixed size elements, but wasn’t sure if the situation would be the same for string arrays.

As append resizes internally a few times I doubt you would notice the difference.

I am attempting to use the MBS LDAP Search. I have that working but I cannot quite get my head wrapped around the solution mentioned by Will (above) in this thread to handle value arrays returned in the dictionary.

Essentially I have a loop with a set of keys in a dictionary returned by the MBS function but a few of the keys return values as variant arrays not just a simple string. Note that the dictionary returned is also an array of dictionaries (hence the subscript for results(0) below) but I don’t get but one dictionary returned (element zero). I test for a returned dictionary array and treat it as an error elsewhere in the code after doing the MBS LDAP search.

Can somebody put the function above to re-cast the variant array to a string array in context in a little code where I would loop through the value array?

Parts of the code below is essentially copied from the MBS LDAP demo program. That all works but I just need to process the return values that are variant arrays.

Dim l as new LDAPMBS("", 3268)
l.SimpleBind("AD\" + Session.ServAcctID, Session.ServAcctPW)
Dim results() as Dictionary = l.Search(BaseDN, Scope, Filter)

'Loop the keys value pairs starting with 1
'  not zero since the key in element zero is always nil.
'Maybe how this LDAP works - not sure.
for i = 1 to results(0).Count - 1
    'System.DebugLog("Key (" + results(0).Key(i).StringValue + _
        ")  Value(" + results(0).Value(i).StringValue + ")")
    if results(0).Key(i) <> nil then
        System.DebugLog("Key (" + results(0).Key(i).StringValue + ")")
	mykey = results(0).Key(i)

        Dim valuelist as Variant = results(0).Value(mykey)
        if valuelist.Type = Variant.TypeString then
            System.DebugLog("--Value ("+results(0).Value(mykey).StringValue+")")
        elseif valuelist.Type = variant.TypeArray then
            'Need to handle valuelist as a string array
            '*** Not Sure How *** right here but need a loop
            ' to process each of the returned values in the array.
        end if

        System.DebugLog("Key " + str(i) + " (nil)")

    end if

next i


if valuelist.Type = variant.TypeArray + Variant.TypeString then dim s() as string = valuelist end if

or for variant array:

if valuelist.Type = variant.TypeArray + Variant.TypeObject then dim s() as variant = valuelist end if

Thanks Christian.

As Homer Simpson would say … Doh.

Somehow I though it was more complicated to make the conversion since I don’t play with dictionaries and variants very much.

BUT … Now I can see all of the data as I dig around in LDAP. I think Codd and Date (the inventors of SQL) would likely “vomit” at how it is implemented or maybe how it is used by some organizations. When you do a query it seems to just “regurgitate” lots of data and says “Here … you figure it out”. I am pretty sure some of the nature of LDAP is due to how it is implemented and how it evolves in a less structured way in each organization.

Thanks again for the help … and the MBS LDAP class that lets me dig around in my “garbage dump” of data. ;-))

You can tell LDAP what attributes to return in a query
look for LDAP 101