Quote Originally Posted by LaVolpe View Post
You'll want to zero out the tColElem when the loop ends. Can do this with an array of LenB(tColElem) or FillMemory API.
Here are a couple of alternative methods:

Code:
CopyMemory ByVal VarPtr(tColElem), ByVal String$(LenB(tColElem), 0), LenB(tColElem)


Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByRef Destination As Any, ByVal Length As Long)

ZeroMemory ByVal VarPtr(tColElem), LenB(tColElem)
BTW, the Key member of the CollectionElement UDT is actually a BSTR (there's a length field and null terminator just before & after the pointed to string data), so the following lines are completely unnecessary:

Code:
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Any) As Long

Dim sKey As String

sKey = String$(lstrlenW(tColElem.Key), 0)
CopyMemory ByVal StrPtr(sKey), ByVal tColElem.Key, lstrlenW(tColElem.Key) * 2
The assignment to the string array can therefore be simplified to:

Code:
CopyMemory ByVal VarPtr(tColElem), ByVal tColElem.PtrNext, LenB(tColElem)
asKeys(lIdx) = tColElem.Key
after changing the CollectionElement.Key's data type from Long to String.