I need help on printing the contents of an array to a txt file in VB6!!!!

piski

Senior member
Jan 21, 2002
312
0
0
This is my statement:


If (MsgBox("Are you sure?", vbYesNo) = vbYes) Then


dlgSave.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
dlgSave.DialogTitle = "Select Your File?"
dlgSave.FileName = ""

dlgSave.ShowSave
dlgSave.InitDir = App.Path

intFileNum = FreeFile
Open dlgSave.FileName For Output As #intFileNum
I = 1
For I = LBound(MyArray) To UBound(MyArray)
ReDim Preserve MyArray(I)
Print #intFileNum, MyArray(I).A, MyArray(I).B, MyArray(I).C, MyArray(I).D, MyArray(I).E, MyArray(I).F
Next I
Close #intFileNum
End If

i run the program and choose the output file that iwant. After i choose the file i go and open it to see the output and it is just a line of 0's. Why is this?
thanks
 

Confused

Elite Member
Nov 13, 2000
14,166
0
0
Open dlgSave.FileName For Binary as #intFileNum
I = 1
For I = LBound(MyArray) To UBound(MyArray)
ReDim Preserve MyArray(I)
Put #intFileNum, MyArray(I).A & ", " & MyArray(I).B & ", " & MyArray(I).C & ", " & MyArray(I).D & ", " & MyArray(I).E & ", " & MyArray(I).F & vbCrLf
Next I
Close #intFileNum
End If
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
Why are you redimming the array?
Why are you setting i = 1 ?

The redim is erasing your array as soon as you call it.
AFAIK, the Preserve keyword will only help you when you are increasing the size of your array, but you are redimming it as 1 element so you are deleting all the other elements.

Say your array has 20 elements
Your LBound will be equal to 0(or 1 depending on how you defined your array) and your UBound will be equal to 19.
So the first time through the loop, you are calling Redim Preserve MyArray(0).
This wipes out elements 1-19.
So every time you go through, you are creating a brand new array element that has no value so it defaults to 0.

The code looks like it should work if you take out the Redim Preserve line.