Results 1 to 4 of 4

Thread: Can I declare an array with unknown size?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2001
    Posts
    57

    Unhappy Can I declare an array with unknown size?

    Can I declare an array with unknown size?

    Such as:
    Dim strCode() As String

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Er.. Yes..! Then you use the Redim function to change the size of it at run time.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    yes, and just to add to Buzby's post, use ReDim Preserve to keep all your existing array elements when you extend your array

    i.e.
    Code:
    Dim s() as String 'string array
    Dim i%
    
    for i = 1 to 10
    
    ReDim Preserve s(i)
    
    Next i
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  4. #4
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    I posted this a bit late!

    To add an item to the array you would have to ReDim the array and use "Preserve" to keep the values:

    Code:
    Dim strCode As String
    Dim topVar As Integer
    Dim i As Integer
    Dim YourNum as Integer
    
    For i = 1 To YourNum
             topVar = UBound(strCode)+1
             ReDim Preserve strCode(topVar) As String
             strCode(topVar) = "This is the last item in the array"
    Next
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width