[Gambas-user] Iterating through a class

richard terry rterry at ...1822...
Sat Apr 5 07:33:52 CEST 2008


On Mon, 31 Mar 2008 12:32:48 am Benoit Minisini wrote:
> On dimanche 30 mars 2008, richard terry wrote:
> > Is it possible to interact through a class in code
> > eg cMyclass
> > public sWhatever as string
> > public sNextThing as string
> > public bSomething as boolean.
> >
> > ie can you  somehow printout the values contained in a class without
> > specifically addressing them by name.
> >
> > Maybe a stupid question.
> >
> > Regards
> >
> > Richard T
>
> You can browse the public symbols of a class. Not the private ones, as they
> do not exist anymore once the program is compiled, except for the debugger.
>
> To do that:
>
> DIM hObject AS Object = ... ' <- Your cMyClass instance
> DIM hClass AS Class = Class["cMyClass"]
>
> FOR EACH SymbolName IN hClass.Symbols
>   WITH hClass[SymbolName]
>     IF .Kind = Class.Variable THEN
>       PRINT .Name;; Object.GetProperty(hObject, .Name)
>     ENDIF
>   END WITH
> NEXT
>
> I think that works. Otherwise you will tell me...

Finally got around to trying this with no luck.  AS a simple example I put a 
button on the form and intended to load the names into a list (project 
supplied)

There is no reference what SymbolName should be set to amongst other things. I  
tried:


Public myclass As Class1
Public Sub form_Open()

myclass = New Class1

End 
Public Sub Button1_Click()

Dim SymbolName As String '????
Dim hObject As Object = myclass
Dim hClass As Class = Class["Class1"]  (dies here complaining it is not an 
object)


For Each SymbolName In hClass.Symbols
  With hClass[SymbolName]
    If .Kind = Class.Variable Then
      Print .Name;; Object.GetProperty(hObject, .Name)
    Endif
  End With
Next

End

Any help appreciated.

Richard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: classit-0.0.1.tar.gz
Type: application/x-tgz
Size: 8223 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20080405/f33255e3/attachment.bin>


More information about the User mailing list