D
deltaquattro
Hi,
I'm trying to write a general subroutine to print the contents of an
array/collection to a file . So far I've come up with the following
subroutine:
Function WriteDataToFile(FileName As String, Container As Variant) As
Boolean
'Write each element contained in Container (either an array or a
collection) to file FileName
Dim FNumber As Long, Element As Variant
'Find free file number
FNumber = FreeFile
'Create new file or overwrite existing one
Open FileName For Output As #FNumber
'Write array to file
For Each Element In Container
Print #FNumber, Element
Next
'Close file and exit
Close #FNumber
End Function
Albeit simple, this function is able to handle perfectly
multidimensional arrays and collection of standard and user-derived
type. However, the Print # function doesn't work for an Object data
type, so it doesn't work for arrays/collections which contain Objects.
How can I write a function which is able to write to file the contents
of an array/collection, irrespective of what type of variables it
holds?
My idea: add code into WriteDataToFile which, For Each Element In
Container, determines whether Element is a variable of standard/user
defined data type or an Object. In the first case I use the Print
function, in the second I call a Print method which I will define in
all of the class module I write. Two questions arise:
1. How do I determine if Element is of standard/user defined data type
or Object data type?
2. How do I write the Print method for my Classes? I tried something
like:
' Print Method
Public Function Print(FileUnit As Long)
'here goes code which prints each Property to file, something like:
Print #FileUnit, Name
Print #FileUnit, Surname
..
..
..
End Function
but this didn't work because Print is a reserved keyword, so I renamed
it PrintToFile (btw, is there any workaround which would still allow
me to name the method Print?)
Well, that's all for now, I'll appreciate if you can answer my
questions, or suggest a completely different approach, in case you
think mine is unfeasible. Thanks in advance,
Best Regards
deltaquattro
I'm trying to write a general subroutine to print the contents of an
array/collection to a file . So far I've come up with the following
subroutine:
Function WriteDataToFile(FileName As String, Container As Variant) As
Boolean
'Write each element contained in Container (either an array or a
collection) to file FileName
Dim FNumber As Long, Element As Variant
'Find free file number
FNumber = FreeFile
'Create new file or overwrite existing one
Open FileName For Output As #FNumber
'Write array to file
For Each Element In Container
Print #FNumber, Element
Next
'Close file and exit
Close #FNumber
End Function
Albeit simple, this function is able to handle perfectly
multidimensional arrays and collection of standard and user-derived
type. However, the Print # function doesn't work for an Object data
type, so it doesn't work for arrays/collections which contain Objects.
How can I write a function which is able to write to file the contents
of an array/collection, irrespective of what type of variables it
holds?
My idea: add code into WriteDataToFile which, For Each Element In
Container, determines whether Element is a variable of standard/user
defined data type or an Object. In the first case I use the Print
function, in the second I call a Print method which I will define in
all of the class module I write. Two questions arise:
1. How do I determine if Element is of standard/user defined data type
or Object data type?
2. How do I write the Print method for my Classes? I tried something
like:
' Print Method
Public Function Print(FileUnit As Long)
'here goes code which prints each Property to file, something like:
Print #FileUnit, Name
Print #FileUnit, Surname
..
..
..
End Function
but this didn't work because Print is a reserved keyword, so I renamed
it PrintToFile (btw, is there any workaround which would still allow
me to name the method Print?)
Well, that's all for now, I'll appreciate if you can answer my
questions, or suggest a completely different approach, in case you
think mine is unfeasible. Thanks in advance,
Best Regards
deltaquattro