Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing an array at runtime in VB.NET

Tags:

arrays

vb.net

In my Windows Forms application at runtime I will be resizing an array each time I add an element. So first I must resize to the size + 1, and then add a member to this index. How do I do this?

like image 872
Alex Gordon Avatar asked Jun 08 '09 22:06

Alex Gordon


People also ask

How do you modify array size in runtime?

Size of an array Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array. Still if you try to assign value to the element of the array beyond its size a run time exception will be generated.

How do you change the size of an array in Visual Basic?

You can use the ReDim statement to change the size of one or more dimensions of an array that has already been declared. If you have a large array and you no longer need some of its elements, ReDim can free up memory by reducing the array size. On the other hand, if your array needs more elements, ReDim can add them.

What is the time complexity of resizing an array?

Specific operations like resize() increases or decreases the size of Dynamic Array but in doing so it needs no extra memory. Hence, the time complexity of resize operation is O(1) irrespective of the total size of Dynamic Array varies.

How do you resize an array?

The easiest way to resize an array is to create a new array with the desired size and copy the contents of the old array into it. We can delete the old array by setting it to null. For this, we use the arraycopy() method of the System class or copyOf() method of the java.


4 Answers

You could use the ReDim statement, but this really isn't your best option. If your array will be changing sizes often, especially as it sounds like you're just appending, you should probably use a generic List(Of T) or similar collection type.

You can use it just like you use an array, with the addition that adding an item to the end is as easy as MyList.Add(item)

To use a generic list, add Imports System.Collections.Generics to the top of the file. Then, you would declare a new integer list like this:

Dim MyList As New List(Of Integer)()

or a string list like this:

Dim MyList As New List(Of String)()

You should get the idea.

like image 141
Joel Coehoorn Avatar answered Sep 21 '22 18:09

Joel Coehoorn


The suggested ReDim's need the Preserve keyword for this scenario.

ReDim Preserve MyArray(n)
like image 32
Jules Avatar answered Sep 21 '22 18:09

Jules


Using a generic list is (as suggested) the best idea. If you however want to change the size of an Array, you can use Array.Resize(ByRef arr, newSize).

ReDim is not a good (pretty bad) idea (VB specific legacy, extremely slow).

like image 44
Corniel Nobel Avatar answered Sep 18 '22 18:09

Corniel Nobel


You can also make your own collection class. A good programming exercise for new programmers.

Public Class MyList
Private Items() As String
Private No As Integer = 0
Public Sub Add(ByVal NewItem As String)

    ''Create a temporary new string array

    Dim CopyString(No) As String

    ''Copy values from Global Variable Items() to new CopyString array

    For i As Integer = 0 To No - 1
        CopyString(i) = Items(i)
    Next

    ''Add new value - NewItem - to CopyString

    CopyString(No) = NewItem

    ''Increment No to No + 1

    No += 1

    ''Copy CopyString to Items

    Items = CopyString

    'Discard CopyString

    CopyString = Nothing

End Sub
Public Sub Show(ByVal index As Integer)
    MsgBox(Items(index))
End Sub
End Class

''Now create a form with a TextBox name - txt, Button1 and Button2

Public Class Form1

''Declare txts as a new MyList Class

Private txts As New MyList

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ''Add text to txts which is a MyList Class

    txts.Add(txt.Text)
    txt.Text = ""

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ''Display value at a specific index

    txts.Show(Convert.ToInt16(txt.Text))
    txt.Text = ""

End Sub
End Class
like image 37
Gaurang Avatar answered Sep 20 '22 18:09

Gaurang