Dynamically setting integer arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Dynamically setting integer arrays

  1. #1
    Join Date
    Mar 2005
    Posts
    4

    Dynamically setting integer arrays

    OK, this is most likely very easy for most of you guys out here, but It is proving most difficult to me.
    IN VB.net, i am trying to add to an integer array. Since I do not know how many items will be in this array when the user is finished I can not set the number of items in the array, i.e. dim intNumber(3) as integer.
    I am trying to write a procedure that will insert a number into the next available dimension of the integer array.
    When I use array.getLength, or array.getupperbound, or array.lenght i keep getting errors about how I can't do this to a null array. I need my program to add the first item to the array once the user enter to correct information, and the next item to the array when they do it again, and so forth until they are done. How can this be accomplished best? Thanks for your help.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The simplest solution is to use an ArrayList (from the System.Collections namespace); you may simply Add items to an ArrayList, and it grows to accomodate them.

    If you must use an array rather than an ArrayList, pick an arbitrary initial size, say, 10 elements, then use ReDim Preserve to grow the array as necessary:
    Code:
    ' Create space for 10 elements, 
    ' numbered 0 through 9
    Dim intNumber(9) As Integer
    ' Keep track of maximum index number
    Dim maxIndex As Integer
    
    ' Now we're going to add 25 elements
    ' to the array
    For I As Integer = 0 To 24
        ' If array is full...
        If maxIndex = UBound(intNumber) Then
            ' Make room for 10 more elements
            ReDim Preserve intNumber(maxIndex + 10)
        End If
        intNumber(maxIndex) = I
        ' Increment max index
        maxIndex += 1
    Next
    
    ' Shrink array to actual number 
    ' of elements
    ReDim Preserve intNumber(maxIndex - 1)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Physical Address of the NIC
    By eduardoms in forum Security
    Replies: 2
    Last Post: 05-06-2005, 11:26 AM
  2. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  3. Replies: 6
    Last Post: 05-18-2001, 12:44 AM
  4. Replies: 0
    Last Post: 05-16-2001, 06:24 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center