|
-
Jul 13th, 2000, 03:04 PM
#1
Thread Starter
Addicted Member
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
-
Jul 13th, 2000, 03:09 PM
#2
Junior Member
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
-
Jul 13th, 2000, 03:10 PM
#3
Hyperactive Member
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
-
Jul 13th, 2000, 03:10 PM
#4
Fanatic Member
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!
-
Jul 13th, 2000, 03:15 PM
#5
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|