I wasn't proposing having an array of UDT's. I was proposing having a
single UDT which holds the array of strings. That way the array, being
part of the UDT, will be dimensioned automatically, as are the strings in
the array. My example saves and restores a two dimension array of
dynamic strings, contained in a single UDT.
As you noted, if you save an array, or strings outside of a UDT, then
you will have to know the dimensions of the array, or the lengths of the
strings, and pre-define them, in order for the "get" to return the proper
number of bytes to fill the array or strings.
Putting the array, and/or strings inside a UDT, allows Visual Basic to
keep track of the dimension of the arrays and the length of the strings.
Visual Basic will then dimension and fill the arrays and strings (in the
UDT) based on what was written to the file (for that UDT).
For an example of what FireXtol is talking about, I've modified the
example to use an array of UDTs. It's an array of 3 elements. Each
element (UDT) has a different sized 2 dimensional array of dynamic
strings in it (First element is 3x5, second element is 2x4, third element is
4x4). Since we are writing the array of UDTs to the file, we must
dimension the array (Dim NewAry(1 To 3) As StringArrayType) when we
want to read the array back from the file, because that information is
not saved in the file. But all the arrays, and strings within the UDTs are
restored properly, because the array dimensions and string sizes are
stored, in addition to the data, within the UDTs.
Here's the modified code.
Code:
Private Type StringArrayType 'Define a User Defined Type (UDT)
str() As String 'to hold a dynamic array of dynamic strings
End Type
Private Sub Command1_Click()
Dim I As Integer, J As Integer
Dim StrAry(1 To 3) As StringArrayType 'Create a variable of the Dynamic string array type
ReDim StrAry(1).str(1 To 3, 1 To 5) 'This element will contain a 3 x 5 array of strings
ReDim StrAry(2).str(1 To 2, 1 To 4) 'This element will contain a 2 x 4 array of strings
ReDim StrAry(3).str(1 To 4, 1 To 4) 'This element will contain a 4 x 4 array of strings
'For each element in our array of 3 UDT's
' For whatever bounds our first dimension has
' For whatever bounds our second dimension has
' Set the String in the two dimensional array to a string identifying
' which UDT, 1st index and 2nd index of this string
' Next element in our second dimension
' next element in our first dimension
'next UDT in our array of UDT's
For k = 1 To 3
For I = LBound(StrAry(k).str, 1) To UBound(StrAry(k).str, 1)
For J = LBound(StrAry(k).str, 2) To UBound(StrAry(k).str, 2)
StrAry(k).str(I, J) = "UDT #" & str$(k) & ": I=" & str$(I) & ", J=" & str$(J)
Next
Next
Next
Open "c:\tst.dat" For Binary As #1 'open a file for binary input/output
Put #1, , StrAry ' Write the array of UDTs to the file
Close #1 'Close the file
End Sub
Private Sub Command2_Click()
Dim I As Integer, J As Integer
Dim NewAry(1 To 3) As StringArrayType 'Declare an array of UDT's the same dimensions as was stored in the file
Open "c:\tst.dat" For Binary As #1 'Open the file
Get #1, , NewAry ' Get the array
Close #1 'Close the file
'For each element in our array of 3 UDT's
' For whatever bounds our first dimension has
' For whatever bounds our second dimension has
' Print the string in the UDT, defined by the two dimensional indexes
' Next element in our second dimension
' next element in our first dimension
'next UDT in our array of UDT's
For k = 1 To 3
For I = LBound(NewAry(k).str, 1) To UBound(NewAry(k).str, 1)
For J = LBound(NewAry(k).str, 2) To UBound(NewAry(k).str, 2)
Debug.Print NewAry(k).str(I, J)
Next
Next
Next
End Sub
Now this is a more complex situation than I usally deal with.
My original point was to use a single UDT to hold the two dimensioned
array of strings and do a single get or put to save and restore the
data.
But, as you can see here, even with this complexity (an array of UDTs,
with differing dimensioned arrays of strings), it still comes down to a
single Get or Put, to save or restore the data, you just need to keep
track of the size of the array yourself (you could put that information
into the file yourself, rather than hardcode it, but I'm not going to go
into how to do that. You would come up with a scheme to write and
read the additional information you need to the file).