[Gambas-user] refer to an object with a string

Doriano Blengino doriano.blengino at ...1909...
Sun Aug 16 18:20:54 CEST 2009


Jean-Yves F. Barbier ha scritto:
> Hi list,
>
> I'm trying to refer to come couples Label/CheckBox that have the same suffix
> (lbl_ONE & ckb_ONE) to enable/disable couple in one shot.
>
> But after building my strings that are orders, GB tell me its not an object:
>
> PUBLIC SUB lblckb_on_off(which AS String, on AS Boolean)
>   DIM lbl AS String
>   DIM ckb AS String
>   
>   lbl = "lbl_" & which & ".Enabled = "
>   ckb = "ckb_" & which & ".Enabled = "
>   IF onoff THEN   ' ON
>     lbl &= "TRUE"
>     ckb &= "TRUE"
>     (SOMETHING TO "EXEC" THE STRING)lbl    (or may be: (SOMETHING)lbl = TRUE)
>     (SOMETHING TO "EXEC" THE STRING)ckb
>   ENDIF 
> END 
>
> Is there a way to do that?
>   
The EVAL statement, but I never used it and also I am not sure it exists 
in gambas2.
But there are other ways, more efficient. One is to find object handlers 
using introspection. You can find all the children of a form and, based 
on their name, do things (you can also scan all the children and, when 
you find the one which has the right name, do the thing you want). The 
other one, which I would recommed, is to create dynamically you objects, 
especially if they are many, and keep a reference to them using arrays 
or collections. Or a mix of the two: you create a form visually in the 
IDE, then in a routine collect all the handlers, and from then on you 
always use those handlers. I suspect you are doing something very large...

Regards,

-- 
Doriano Blengino

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list