LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default How to write to file the contents of a generic array/collection

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic file path using vba dwake Excel Programming 2 January 27th 09 11:09 PM
Array vs. Collection RyanH Excel Programming 7 October 24th 08 06:18 PM
check if file with generic name already open Spike Excel Programming 2 November 25th 05 11:03 PM
write 1-dimensional array of integers to text file RB Smissaert Excel Programming 3 October 12th 03 05:56 PM
Write to Array from other closed Excel file Dave B[_4_] Excel Programming 5 October 1st 03 04:48 PM


All times are GMT +1. The time now is 02:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"