Ciao Peter!
Yes, you got my point! I want to write text to a file: the text is the
contents of the various objects/used-defined types which are contained
in an array or a collection.
If I used an older programming language such as Fortran, I would have
to write a a different subroutine for each of these:
- write to file the contents of a 1D array containing Long;
- write to file the contents of a 2D array containing Long;
- ...
- a 1D array containing Double;
- ...
- a Collection containing Cfoo (Objects of a class written by me);
and so on. In VBA, instead, I hoped to be able to write a single
subroutine which works for all of them. Now, as you correctly point
out, the simple loop
For Each Element In Container
Print #FNumber, Element
Next
works fine in a lot of cases, since standard data types and most user-
defined data types (at least, all the ones I defined so far) are
coercible to text. This doesn't work for Objects of a class defined by
me, though, because I don't define default properties for them.
Defining a default property is not possible with the VBE of Excel, but
it can be done exporting the class module to text and using a text
editor:
http://www.cpearson.com/excel/DefaultMember.aspx
However, even if I did that, that wouldn't solve my issue, since I
want to write to file *all* the contents of each Object, not just its
default property. So I thought of the following scheme:
1. For each Element of the Container, I check whether it is an Object
or not:
For Each Element In Container
' Some way to check if Element is of Object data type or not
If NotAnObject Then
Print #FNumber, Element
Else
Element.PrintToFile(#FNumber)
End If
Next
So, first question is: how do I check if a variable is of Object data
type or not? Even better, can I check if it belong to a Class which I
defined, or if it's a standard Excel Object, such as a Range?
2. Second question: I need to add a PrintToFile method to each of the
Classes I define, which prints out to file all the properties of the
Object. How do I do that? 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?). Is this a good way? Is there a more
general/easier/better way? Thanks
Best Regards,
Sergio Rossi
On 21 Apr, 17:48, "Peter T" <peter_t@discussions wrote:
I'm not really following what you are trying to do but tat the end of the
day you need to write text in each loop.
For Each Element In Container
* * Print #FNumber, Element
Next
This will only work if Element defaults to text, even if a little coercion
is required (if say a number). *It would work if say Element is a single
cell range object and Container a larger range of cells.
If Element is an object, and it's default property is not text (or easily
coercible to text) you need to include whatever property or set of
properties is required to return the text. It might be something as simple
as
For Each Element In Container
* * Print #FNumber, Element.Text
Next
But it really depends on the object and in particular it's default property.
Regards,
Peter T
"deltaquattro" wrote in message
...