Hi Folks: I am trying to write a program where I have to maintain a file that will save the current state of a global “CurrentState” object. The Current State object has several collections of other objects (along with singleton primitives and objects) each of which may contain other collections of other objects (or primitives) and/or singleton objects and/or primitive properties.
Now, for each object type I have I can create a “WriteToStateFile” method that will write each primitive property value of the object to the global statefile, but I want a method that does this without knowing what the object’s properties are. i.e. imagine I have two methods – one called WriteObjectToStateFile and another called WritePrimitiveToStateFile and I have some Object called obj which I know may contain both objects and primitives. I want to know how to go through all the properties of obj, determine which are Objects in their own right and which are primitives, then call the appropriate Write method based upon that determination. My problem is knowing how many properties my original obj has and how to access them in a generic manner.
Class SomeClass
aMemberClass as AnotherClass
aStringPrimitive as String
anIntPrimitive as Integer
””’
Define sets/gets/lets blah blah
End Class
Public Sub WriteToStateFile (Obj As Object)
Dim var, i as Integer
Set var = New Obj.CollectionOfProperties ‘I wish this existed for any user defined class (could i define it somehow?)
For i = 1 to var.Count ‘Then I could do this
if IsObject(var(i)) then
WriteToStateFile var(i) ‘Note the recursive call
else
WritePrimitiveToStateFile var(i)
end if
Next i
End Sub
Private Sub WritePrimitiveToStateFile (prim as Variant)
OpenStateFileForAppend
‘if necessary do a select on typeof prim to determine the primitive type and then…
StateFile.AppendLine prim
StateFile.Close
End Sub
SO my problem is – is there some means of getting this Obj.listOfProperties in a generic manner rather than going through a TypeOf Obj scenario where I still end up writing all the object type specific routines.
(and forgive me if i seem to be asking the inane q’s of a newbie…since i am not a pro)
regards,
Lup