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,
target
will be counted from the end. Iftarget
is at or greater than the array’sLength
property, nothing will be copied. Iftarget
is positioned afterStartIndex
, the copied sequence will be trimmed to fit the array’sLength
property.
- Name
StartIndex
- Type
Long
- Necessity
- Optional
- Description
- The index at which to start copying elements from. If negative,
StartIndex
will be counted from the end. IfStartIndex
is omitted,CopyWithin
will copy from the LowerBound index of the array.
- Name
EndIndex
- Type
Long
- Necessity
- Optional
- Description
- The index at which to end copying elements from.
CopyWithin
copies up to but not includingEndIndex
. If negative,EndIndex
will be counted from the end. IfEndIndex
is omitted,CopyWithin
will copy until the last index (default to the array’sLength
property).
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