SortedList: CopyTo

CopyTo

Copies SortedList elements to a one-dimensional array, starting at the specified index in the array.



 Public Sub CopyTo(
	  ByRef Arr As Variant,
	  ByVal ArrayIndex As Long )

Parameters

Arr
[ByRef] Variant. The one-dimensional array that is the destination of the DictionaryEntry objects copied from SortedList.
ArrayIndex
[ByVal] Long. The starting index in Arr to begin copying to.

Remarks

The key/value pairs are copied to the array in the same order in which the enumerator iterates through the SortedList object.

To copy only the keys in the SortedList, use SortedList.Keys.CopyTo.

To copy only the values in the SortedList, use SortedList.Values.CopyTo.

Exceptions

ExceptionCondition
ArgumentNullException Arr is uninitialized.
ArgumentOutOfRangeException ArrayIndex is less than the lower-bound of Arr.
ArgumentException Arr is multidimensional.
-or-
The number of elements in the source SortedList object is greater than the available space from ArrayIndex to the end of the destination array.
InvalidCastException The type of the source SortedList cannot be cast automatically to the type of the destination array.

Examples

The following code example shows how to copy the values in a SortedList object into a one-dimensional array.

Public Sub Main()
    Dim List As New SortedList
    Dim TempArray() As String
    Dim TargetArray() As DictionaryEntry
    Dim i As Long
    
    List.Add 2, "cats"
    List.Add 3, "in"
    List.Add 1, "napping"
    List.Add 4, "the"
    List.Add 0, "three"
    List.Add 5, "barn"
    
    TempArray = NewStrings("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog")
    ReDim TargetArray(0 To 14)
    
    For i = 0 To UBound(TargetArray)
        If i <= UBound(TempArray) Then
            Set TargetArray(i) = NewDictionaryEntry(i, TempArray(i))
        Else
            Set TargetArray(i) = NewDictionaryEntry(i, "")
        End If
    Next
    
    Debug.Print "The target Array contains the following (before and after copying):"
    PrintValues TargetArray
    
    List.CopyTo TargetArray, 6
    
    PrintValues TargetArray
End Sub

Private Sub PrintValues(ByRef Arr() As DictionaryEntry)
    Dim i As Long
    
    For i = LBound(Arr) To UBound(Arr)
        Debug.Print " "; Arr(i).Value;
    Next
    
    Debug.Print
End Sub

' The code produces the following output.
'
'    The target Array contains the following (before and after copying):
'     The quick brown fox jumped over the lazy dog
'     The quick brown fox jumped over three napping cats in the barn

See Also

Project CorLib Overview

Class SortedList Overview

DictionaryEntry