locked
how can I set a value for an unlimited array of integer? RRS feed

  • Question

  • hi 

    I want to define an unlimited array of integer and add values to it but I have some problems.

    I'm using this code (for example):

    Dim I() As Integer
    I(1) = 1

    what should I do?

    Saturday, September 3, 2011 11:37 AM

Answers

  • As mentioned arrays must have a size (or just be null references, not pointing at any array object). Once you create an array object you can't resize it either - you have to create a new array object. Methods such as Array.Resize, and the ReDim keyword in VB.Net are just creating new array objects and copying the old data over. This can be quite an expensive operation.

    If you want a collection of object that can grow in size, then you could resize the array every time you want to increase its size. But - it's going to be inefficient if you are resizing often. Another strategy is to double the size of the array when you need more room, and keep track of how much room there is in the array and how many of the array slots you are currently using - this way you cut down the number of inefficient resize operations you have to do. This is quite a lot of code, and very re-usable, which is why microsoft have done it for you - in the ArrayList and List(Of T) classes. Don't use the ArrayList though, the List(Of T) is better in virtually all scenarios.

    So - use an array if you know how many things you are going to have and it doesn't change much. Use a List(Of T) if it's going to have to grow or shrink in the number of elements.

    The List(Of T) isn't always the best choice either - if you consider what the collection is going to be used for you might find a more efficient or easier-to-use collection type: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

    • Proposed as answer by Reed KimbleMVP Saturday, September 3, 2011 4:51 PM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 1:25 PM
  • 'unlimited' is impossibile. 'indefinited' (initially) is possible. You can declare an array without size, as
    Dim i As Integer()
    

    and then resize the array
    Array.Resize(i, newLenght)
    
    
     

    then you can assign some integer value to any of the elements

    i(1) = 1
    

    keep in mind that indexing of all collections starts from zero


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Proposed as answer by John Anthony Oliver Saturday, September 3, 2011 11:54 AM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 11:51 AM
  • Hi,

    You can not have an unlimited array.

    They say only the space in the universe is infinite or unlimited ( only God knows the answer to whether that is TRUE ).

    Your computers memory and hard-drive space and VB.Net does not allow it.

    You can however have a very large array.

     

    It is far easier to work with a LIST in my opinion.

     

    Please read my code comments below.

     

    Public Class Form1
    
        Private I As New List(Of Integer)
        Private iArray() As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.Show()
    
            'Here I add the numbers 1 million down to zero into the LIST named "I" :>>
            For index As Integer = 100000 To 0 Step -1
                I.Add(index)
            Next
    
            Dim someNumber As Integer
    
            'To retrieve a value use a LIST like this.>>
            someNumber = I.Item(234)
            'or you can use:>>
            someNumber = I(234)
    
            MessageBox.Show(someNumber.ToString)
    
            'If you want to copy a LIST to an ARRAY use .ToArray >>
            iArray = I.ToArray
    
            'If you want to copy an ARRAY to a LIST use .ToList >>
            I = iArray.ToList
    
            'A LIST is just as easy to work with as an array in my opinion.  :-)
    
        End Sub
    
    End Class
    




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    • Proposed as answer by Reed KimbleMVP Saturday, September 3, 2011 4:50 PM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 11:52 AM

All replies

  • If there's no special reason, i'd use collections such as ArrayList not to care of struggling with array size then use ToArray method of ArrayList if you want an array at the end:

    Dim arrlist As New System.Collections.ArrayList

    arrlist.Add(1)

    ' Define an array
    Dim I() As Integer
    I = arrlist.ToArray(GetType(Integer))

    ' See the proof
    MsgBox(I(0).ToString) ' proof outputs -> "1"

    ToArray method:

    http://msdn.microsoft.com/en-us/library/fcyyh2hb.aspx

    If you still want to use arrays rather than collections, you need to initialize array by specifying a pre-set size not to get null reference exception.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)


    • Edited by Onur Güzel Saturday, September 3, 2011 11:47 AM
    Saturday, September 3, 2011 11:47 AM
  • 'unlimited' is impossibile. 'indefinited' (initially) is possible. You can declare an array without size, as
    Dim i As Integer()
    

    and then resize the array
    Array.Resize(i, newLenght)
    
    
     

    then you can assign some integer value to any of the elements

    i(1) = 1
    

    keep in mind that indexing of all collections starts from zero


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Proposed as answer by John Anthony Oliver Saturday, September 3, 2011 11:54 AM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 11:51 AM
  • Hi,

    You can not have an unlimited array.

    They say only the space in the universe is infinite or unlimited ( only God knows the answer to whether that is TRUE ).

    Your computers memory and hard-drive space and VB.Net does not allow it.

    You can however have a very large array.

     

    It is far easier to work with a LIST in my opinion.

     

    Please read my code comments below.

     

    Public Class Form1
    
        Private I As New List(Of Integer)
        Private iArray() As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.Show()
    
            'Here I add the numbers 1 million down to zero into the LIST named "I" :>>
            For index As Integer = 100000 To 0 Step -1
                I.Add(index)
            Next
    
            Dim someNumber As Integer
    
            'To retrieve a value use a LIST like this.>>
            someNumber = I.Item(234)
            'or you can use:>>
            someNumber = I(234)
    
            MessageBox.Show(someNumber.ToString)
    
            'If you want to copy a LIST to an ARRAY use .ToArray >>
            iArray = I.ToArray
    
            'If you want to copy an ARRAY to a LIST use .ToList >>
            I = iArray.ToList
    
            'A LIST is just as easy to work with as an array in my opinion.  :-)
    
        End Sub
    
    End Class
    




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    • Proposed as answer by Reed KimbleMVP Saturday, September 3, 2011 4:50 PM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 11:52 AM
  • As mentioned arrays must have a size (or just be null references, not pointing at any array object). Once you create an array object you can't resize it either - you have to create a new array object. Methods such as Array.Resize, and the ReDim keyword in VB.Net are just creating new array objects and copying the old data over. This can be quite an expensive operation.

    If you want a collection of object that can grow in size, then you could resize the array every time you want to increase its size. But - it's going to be inefficient if you are resizing often. Another strategy is to double the size of the array when you need more room, and keep track of how much room there is in the array and how many of the array slots you are currently using - this way you cut down the number of inefficient resize operations you have to do. This is quite a lot of code, and very re-usable, which is why microsoft have done it for you - in the ArrayList and List(Of T) classes. Don't use the ArrayList though, the List(Of T) is better in virtually all scenarios.

    So - use an array if you know how many things you are going to have and it doesn't change much. Use a List(Of T) if it's going to have to grow or shrink in the number of elements.

    The List(Of T) isn't always the best choice either - if you consider what the collection is going to be used for you might find a more efficient or easier-to-use collection type: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

    • Proposed as answer by Reed KimbleMVP Saturday, September 3, 2011 4:51 PM
    • Marked as answer by Kee Poppy Friday, September 9, 2011 10:40 AM
    Saturday, September 3, 2011 1:25 PM