Alex Gordon
Alex Gordon

Reputation: 60731

Resizing an array at runtime in 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?

Upvotes: 12

Views: 80478

Answers (9)

John89
John89

Reputation: 57

As stated by Joel Coehoorn (the accepted answer) you should just use a list, but you if you REALLY want to use an array you could do something like this:

Imports System.Runtime.CompilerServices

Module Foo
    ''' <summary>
    '''     Adds the specified object to the end of the collection.
    ''' </summary>
    ''' <param name="array">
    '''     The object collection to append.
    ''' </param>
    ''' <param name="value">
    '''     The object to add to the end of the collection.
    ''' </param>
    <Extension> ' Attribute Info: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.extensionattribute?view=net-8.0 
    Public Sub Add(ByRef array As Object(), value As Object)
        ' Optional:
        ' Dim aType As Type = array.GetType.GetElementType
        ' Dim vType As Type = value.GetType
        ' If aType <> vType Then Throw New ArrayTypeMismatchException()
        Dim length As Integer = array.Count
        ReDim Preserve array(length)
        array(length) = value
    End Sub
End Module

Upvotes: 0

Juver Paredes
Juver Paredes

Reputation: 134

This work for me

    Dim Table1 As New DataTable
    ' Define columns
    Table1.Columns.Add("Column1", GetType(System.String))
    Table1.Columns.Add("Column2", GetType(System.Int32))
    Table1.Columns.Add("Column3", GetType(System.Int32))
    ' Add a row of data
    Table1.Rows.Add("Item1", 44, 99)
    Table1.Rows.Add("Item2", 42, 3)
    Table1.Rows.Add("Item3", 42, 3)
    Table1.Rows.Add("Item4", 42, 3)
    Dim arr(-1) As String
    For Each dr As DataRow In Table1.Rows
        ReDim Preserve arr(arr.Length)
        arr(arr.Length - 1) = dr("Column1")
    Next

Upvotes: 0

Corniel Nobel
Corniel Nobel

Reputation: 429

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).

Upvotes: 9

Gaurang
Gaurang

Reputation: 87

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

Upvotes: 2

tekBlues
tekBlues

Reputation: 5793

I would prefer some type of collection class, but if you WANT to use an array do it like this:

Dim arr() As Integer
Dim cnt As Integer = 0
Dim ix As Integer

For ix = 1 To 1000
    cnt = cnt + 1
    ReDim arr(cnt)
    arr(cnt - 1) = ix
Next

Upvotes: 3

dsas
dsas

Reputation: 1660

As Joel says, use a list.

Dim MyList As New List(Of String)

Don't forget to change Of String to be Of whichever datatype you're using.

Upvotes: 0

Jules
Jules

Reputation: 4339

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

ReDim Preserve MyArray(n)

Upvotes: 9

Dillie-O
Dillie-O

Reputation: 29725

Use the ReDim command to specify the new size.

ReDim MyArray(MyArray.Length + 1)

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

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.

Upvotes: 29

Related Questions