Home |
Search |
Today's Posts |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You seem to be asking for some generic routine that will return all text
type properties of any arbitrary object. Whilst it might be theoretically possible (with an incredible amount of work) if the object is defined in a type library, it's not viable. Obviously there will be nothing defined in a library about your own objects class objects. Take the cell (range object), it has Value, Value2, Text, ID etc. So apart from the default you need to get each by property-name. You might even need to dig deeper into the object. As for your own objects why not include a method to get all text properties, maybe something like this ' Class1 Private msName As String Private msAddress As String Private mnID As Long Public Property Let Employee(sName, sAddress, nID) mnID = nID msName = sName msAddress = sAddress End Property Public Function GetHeaders() As String GetHeaders = "ID" & vbTab & "Name" & vbTab & "Address" End Function Public Function GetProps() As String GetProps = CStr(mnID) & vbTab & msName & vbTab & msAddress End Function ' Normal module Sub test() Dim col As Collection Dim C As Class1 Set col = New Collection For i = 1 To 2 Set C = New Class1 C.Employee("Tom-" & i, i & " Main St") = i * 100 col.Add C, CStr(i) Next Debug.Print col(1).GetHeaders For Each C In col Debug.Print C.GetProps Next End Sub Before starting with your Print function try and get all your properties returned as strings first. Personally I wouldn't bother with setting the default property of a class object, generally better to explicitly get what you are after (that applies to objects in general also). Best never use a keyword as the name of a procedure or variable, so stick with PrintToFile instead of Print. Regards, Peter T "deltaquattro" wrote in message ... 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 ... |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Generic file path using vba | Excel Programming | |||
Array vs. Collection | Excel Programming | |||
check if file with generic name already open | Excel Programming | |||
write 1-dimensional array of integers to text file | Excel Programming | |||
Write to Array from other closed Excel file | Excel Programming |