Results 1 to 5 of 5

Thread: Redim

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    Dubuque, IA
    Posts
    134

    Question

    Whenever the Redim statement adds an array, I lose previous array values. can this be avoided without having to read the file twice?

    example:
    Pretend file(1) is a simple text file
    dim linecnt as integer
    dim StringIn as String, line() as String

    linecnt = 0
    do until eof(1)
    Line Input #1, StringIn
    linecnt = linecnt + 1
    Redim line(linecnt)
    line(linecnt) = StringIn
    loop

  2. #2
    Junior Member
    Join Date
    Jun 2000
    Posts
    28
    I have never actualy tried this, but this is what the msdn help file says

    Preserving the Contents of Dynamic Arrays
    Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values to the Empty value (for Variant arrays), to zero (for numeric arrays), to a zero-length string (for string arrays), or to Nothing (for arrays of objects).

    This is useful when you want to prepare the array for new data, or when you want to shrink the size of the array to take up minimal memory. Sometimes you may want to change the size of the array without losing the data in the array. You can do this by using ReDim with the Preserve keyword. For example, you can enlarge an array by one element without losing the values of the existing elements using the UBound function to refer to the upper bound:

    ReDim Preserve DynArray(UBound(DynArray) + 1)

    Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs. Thus, you can use code like this:

    ReDim Preserve Matrix(10, UBound(Matrix, 2) + 1)
    good luck
    Using VB 6.0 SP 3
    Bed goes up, bed goes down

  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Cool

    Use the PRESERVE function after ReDim

    Code:
    linecnt = 0 
    Do Until EOF(1) 
        Line Input #1, StringIn 
        linecnt = linecnt + 1 
        ReDim Preserve line(linecnt) 
        line(linecnt) = StringIn 
    Loop
    Also, when you post code examples, use the code tags ("code" in brackets [] at the beginning of the code and "/code" in brackets [] at the end). That preserves the formatting and makes it easier to read.

    Suerte

    Andrew

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    You Need to use the Preserve keyword.

    This will redim an array, and you will not lose any of the data.

    Be warned that this takes a relativley long time, so try rediming as little as possible. One way to do this is to redim the array in steps of 10, or 50. Then when you have the final count you can redim it to the exact size.

    Code:
    ''Pretend file(1) is a simple text file 
    dim linecnt as integer 
    dim StringIn as String, line() as String 
    
    linecnt = 0 
    do until eof(1) 
      Line Input #1, StringIn 
      linecnt = linecnt + 1
      Redim Preserve line(linecnt) As String
      line(linecnt) = StringIn 
    loop
    
    
    
    'or to redim it in steps of 50
    Pretend file(1) is a simple text file 
    dim linecnt as integer 
    dim StringIn as String, line() as String 
    
    linecnt = 0 
    Redim line(50) As String
    
    Do Until eof(1) 
      Line Input #1, StringIn 
      linecnt = linecnt + 1
      If linecount mod 50 = 0 then
        Redim Preserve line(linecnt + 50) as String
      End If
      line(linecnt) = StringIn 
    Loop
    
    Redim Preserve line(lineCount) as String
    Iain, thats with an i by the way!

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Lightbulb Goodness!

    3 people posting the same answer, and all within one minute of each other! My, we are an intelligent bunch

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