Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new value to integer array (Visual Basic 2010)

Tags:

arrays

vb.net

I've a dynamic integer array to which I wish to add new values. How can I do it?

Dim TeamIndex(), i As Integer

For i = 0 to 100
    'TeamIndex(i).Add = <some value>
Next
like image 654
antikbd Avatar asked May 14 '12 07:05

antikbd


2 Answers

Use ReDim with Preserve to increase the size of array with preserving old values.

ReDim in loop is advisable when you have no idea about the size and came to know for increasing the Array size one by one.

Dim TeamIndex(), i As Integer

For i = 0 to 100
     ReDim Preserve TeamIndex(i)
    TeamIndex(i) = <some value>
Next

If you to declare the size of array at later in code in shot then use

 ReDim TeamIndex(100)

So the code will be :

Dim TeamIndex(), i As Integer
ReDim TeamIndex(100)
For i = 0 to 100
    TeamIndex(i) = <some value>
Next

You can Use the ArrayList/List(Of T) to use Add/Remove the values more dynamically.

 Sub Main()
    ' Create an ArrayList and add three strings to it.
    Dim list As New ArrayList
    list.Add("Dot")
    list.Add("Net")
    list.Add("Perls")
    ' Remove a string.
    list.RemoveAt(1)
    ' Insert a string.
    list.Insert(0, "Carrot")
    ' Remove a range.
    list.RemoveRange(0, 2)
    ' Display.
    Dim str As String
    For Each str In list
        Console.WriteLine(str)
    Next
    End Sub

List(Of T) MSDN

List(Of T) DotNetperls

like image 65
Romil Kumar Jain Avatar answered Nov 15 '22 05:11

Romil Kumar Jain


There is nothing in Romil's answer that I consider to be wrong but I would go further. ReDim Preserve is a very useful command but it is important to realise that it is an expensive command and to use it wisely.

Consider:

Dim TeamIndex(), i As Integer
For i = 0 to 100
  ReDim Preserve TeamIndex(i)
  TeamIndex(i) = <some value>
Next

For every loop, except i=0, the Common Language Runtime (CLR) must:

  • find space for a new integer array that is one element bigger than the previous array
  • copy the values across from the previous array
  • initialise the new element
  • release the previous array for garbage collection.

ArrayList is fantastic if you need to add or remove elements from the middle of the array but you are paying for that functionality even if you do not need it. If, for example, you are reading values from a file and storing them sequentially but do not know in advance how many values there will be, ArrayList carries a heavy overhead you can avoid.

I always use ReDim like this:

Dim MyArray() As XXX
Dim InxMACrntMax As Integer

ReDim MyArray(1000)
InxMACrntMax=-1

Do While more data to add to MyArray

  InxMACrntMax = InxMACrntMax + 1
  If InxMACrntMax > UBound(MyArray) Then
    ReDim Preserve MyArray(UBound(MyArray)+100)
  End If
  MyArray(InxMACrntMax) = NewValue

Loop                

ReDim MyArray(InxMACrntMax)    ' Discard excess elements

Above I have used 100 and 1000. The values I pick depend on my assessment of the likely requirement.

like image 27
Tony Dallimore Avatar answered Nov 15 '22 03:11

Tony Dallimore