CopyWithin
Description
The CopyWithin() method copies part of an array to another location in the same array without modifying its length. The CopyWithin method takes up to three Parameters target, StartIndex and EndIndex.
Note
The end argument is optional with the length of the this object as its default value. If target is negative, it is treated as length + target where length is the length of the array. If start is negative, it is treated as length + start. If end is negative, it is treated as length + end.
Syntax
expression.CopyWithin(target, [StartIndex], [EndIndex])
Parameters
- Name
target- Type
Long- Necessity
- Required
- Description
- The index at which to copy the sequence to. If negative,
targetwill be counted from the end. Iftargetis at or greater than the array’sLengthproperty, nothing will be copied. Iftargetis positioned afterStartIndex, the copied sequence will be trimmed to fit the array’sLengthproperty.
- Name
StartIndex- Type
Long- Necessity
- Optional
- Description
- The index at which to start copying elements from. If negative,
StartIndexwill be counted from the end. IfStartIndexis omitted,CopyWithinwill copy from the LowerBound index of the array.
- Name
EndIndex- Type
Long- Necessity
- Optional
- Description
- The index at which to end copying elements from.
CopyWithincopies up to but not includingEndIndex. If negative,EndIndexwill be counted from the end. IfEndIndexis omitted,CopyWithinwill copy until the last index (default to the array’sLengthproperty).
Returns
- Type
BetterArray/Object- Description
- The current instance of the BetterArray object with the stored array modified.
Example
Public Sub CopyWithinExample()
Dim MyArray As BetterArray
Dim result() As Variant
Set MyArray = New BetterArray
MyArray.Push "a", "b", "c", "d", "e"
MyArray.CopyWithin 0, 3, 4
result = MyArray.Items
' expected output:
' result is an array with the values: "d", "b", "c", "d", "e"
End Sub