[Gambas-user] Iterating through a class

Benoit Minisini gambas at ...1...
Sun Mar 30 15:32:48 CEST 2008


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...

-- 
Benoit Minisini




More information about the User mailing list