As Heinzi said, the array has a fixed size. To โdelete an elementโ or โresizeโ, you will need to create a new array with the desired size and copy the elements you need.
Here is the code to remove an element from the array:
<System.Runtime.CompilerServices.Extension()> _ Function RemoveAt(Of T)(ByVal arr As T(), ByVal index As Integer) As T() Dim uBound = arr.GetUpperBound(0) Dim lBound = arr.GetLowerBound(0) Dim arrLen = uBound - lBound If index < lBound OrElse index > uBound Then Throw New ArgumentOutOfRangeException( _ String.Format("Index must be from {0} to {1}.", lBound, uBound)) Else 'create an array 1 element less than the input array Dim outArr(arrLen - 1) As T 'copy the first part of the input array Array.Copy(arr, 0, outArr, 0, index) 'then copy the second part of the input array Array.Copy(arr, index + 1, outArr, index, uBound - index) Return outArr End If End Function
You can use it as such:
Module Module1 Sub Main() Dim arr = New String() {"abc", "mno", "xyz"} arr.RemoveAt(1) End Sub End Module
In the above code, the second element ( "mno" ) [which has index 1] is removed from the array.
To use the extension method, you need to develop .NET 3.5 or higher. If you are using .NET 2.0 or 3.0, you can call the method as such
arr = RemoveAt(arr, 1)
Hope this is what you need.
Update
After running the tests based on the comment of ToolMakerSteve , the source code does not change the array you want to update, due to ByVal used in the function declaration. However, writing code like arr = arr.RemoveAt(1) or arr = RemoveAt(arr, 1) changes the array because it reassigns the changed array to the original.
Find below the updated method (subroutine) to remove an element from the array.
<System.Runtime.CompilerServices.Extension()> _ Public Sub RemoveAt(Of T)(ByRef arr As T(), ByVal index As Integer) Dim uBound = arr.GetUpperBound(0) Dim lBound = arr.GetLowerBound(0) Dim arrLen = uBound - lBound If index < lBound OrElse index > uBound Then Throw New ArgumentOutOfRangeException( _ String.Format("Index must be from {0} to {1}.", lBound, uBound)) Else 'create an array 1 element less than the input array Dim outArr(arrLen - 1) As T 'copy the first part of the input array Array.Copy(arr, 0, outArr, 0, index) 'then copy the second part of the input array Array.Copy(arr, index + 1, outArr, index, uBound - index) arr = outArr End If End Sub
Using the method is similar to the original, except this time there is no return value, so trying to assign an array from the return value will not work because nothing is returned.
Dim arr = New String() {"abc", "mno", "xyz"} arr.RemoveAt(1) ' Output: {"abc", "mno"} (works on .NET 3.5 and higher) RemoveAt(arr, 1) ' Output: {"abc", "mno"} (works on all versions of .NET fx) arr = arr.RemoveAt(1) 'will not work; no return value arr = RemoveAt(arr, 1) 'will not work; no return value
Note:
- I use a temporary array for the process because it makes my intentions clear, and that is exactly what VB.NET does behind the scenes when you make
Redim Preserve . If you want to change the array in place using Redim Preserve , see ToolmakerSteve's answer . The RemoveAt methods described here are extension methods. For them to work, you need to insert them into the Module . Extension methods will not work in VB.NET if they are placed in Class .
Important If you are modifying an array with a lot of "deletes", it is strongly recommended that you use a different data structure, such as List(Of T) , as suggested by other respondents for this question.