Combining the answers of @xpda and @Enigmativity, note that Array.Copy can be safely used to copy back to the original array. Quote from the msdn page for the Array.Copy method:
If sourceArray and destinationArray overlap, this method behaves as if the original sourceArray values ββwere stored in a temporary location before the destinationArray file was overwritten.
Here is a routine (extension) that will remove an element at the specified index of an array of any type:
' Remove element at index "index". Result is one element shorter. ' Similar to List.RemoveAt, but for arrays. <System.Runtime.CompilerServices.Extension()> _ Public Sub RemoveAt(Of T)(ByRef a() As T, ByVal index As Integer) ' Move elements after "index" down 1 position. Array.Copy(a, index + 1, a, index, UBound(a) - index) ' Shorten by 1 element. ReDim Preserve a(UBound(a) - 1) End Sub
Usage examples (assuming the array starts at index 0):
a.RemoveAt(0) ' Remove first element a.RemoveAt(1) ' Remove second element. a.RemoveAt(UBound(a)) ' Remove last element
source share