[Gambas-user] Problems with controls - text property

Benoit Minisini gambas at ...1...
Fri Apr 27 00:52:46 CEST 2007


On vendredi 27 avril 2007, ron wrote:
> On Thursday 26 April 2007 23:05, Steven Lobbezoo wrote:
> > Hi,
> >
> > I dont get it, if I do the following :
> > -------------------------------------------------------------------------
> >---- FOR EACH ctrl IN ME.Controls
> >         IF ctrl.Name = tmp THEN
> >           IF ctrl.Tag <> ctrl.Text THEN
> >             ' ok, we've changed something, let's write it back
> >             IF SqlPrt THEN SqlPrt = SqlPrt & ","
> >             SqlPrt = SqlPrt & " " & Object.GetProperty(ctrl, "Name") & "
> > = '" & Object.GetProperty(ctrl, "Text") & "' "
> >           END IF
> >           BREAK
> >         END IF
> >       NEXT
> > -------------------------------------------------------------------------
> >----
> >
> > I get  "Unknown symbol 'test' in class control"
> >
> > Certanly there are  controls in the form that not have a property 'text',
> > but I filter them out with the line "        IF ctrl.Name = tmp THEN   "
> > Since ALL the controls that satisfy this condition have a property
> > "text".
> >
> > If I look into the vars ctrl.Tag and ctrl.Text there is a value, no
> > problem there.
> >
> > So, why do I get this error ?
> >
> > Steven
>
> Because the controls does not have a name property.
>
> What you see "as" name is in the design mode.
>
> This 'name' thing does have a long history of wishes but Benoit
> decided not to implement and suggest to use the 'Tag' property for
> storing your name of the control.
>
>
> BTW
> SqlPrt = SqlPrt & " " & Object.GetProperty(ctrl, "Name") & " = '" &
> Object.GetProperty(ctrl, "Text") & "' " can be
> SqlPrt = SqlPrt & " " & ctrl.Name & " = '" & ctrl.Text & "' "
>
> if the .name was implemented.
>
> The other way is using the useless ID property.
>
> DIM YourTmp as Control
> ' you need next line to get the ID of created control in a fixed way
> YourTmp = new TextBox(Form) as "Tmp"
>
> and use in your Sub
>
>        FOR EACH ctrl IN ME.Controls
>          IF ctrl.ID = YourTmp.ID THEN
>            IF ctrl.Tag <> ctrl.Text THEN
>
> You can also in design mode fill the 'Tag' with the name of the
> control and in the Form.Open or .new read the 'Tag', store it in
> collection and clear the content of 'Tag'
>
> the collection key is the content of the 'Tag' and the value the .ID of the
> control
>
>        FOR EACH ctrl IN ME.Controls
>          IF ctrl.ID = YourIdCollection['tmp'] THEN
>            IF ctrl.Tag <> ctrl.Text THEN
>
> 		SqlPrt = SqlPrt & " " & YourIdCollection.key & " = '" & ctrl.Text & "' "
> 		or
> 		SqlPrt = SqlPrt & " " & "tmp = '" & ctrl.Text & "' "
>
>
>
> Ron
>

Hmm... It is absolutely not that :-)

The Name property has been implemented for a while.

The problem is that Steven didn't understand that if *he* knows that his test 
is true only for control having the Text property, Gambas does not.

Gambas uses static typing with virtual dispatching. 

'Static typing' means that the class of an object is given by the declaration 
of the variable that contains its reference.

'Virtual dispatching' means that the symbol (method or property) applied to 
this reference will call the implementation of the true object class, not the 
class given by the declaration of the variable.

So if the 'ctrl' variable is declared 'AS Control', then you can only use 
symbols of the Control class on it.

If you want Gambas to use dynamic typing, i.e. detects that the Text property 
exists each time it uses it, then you must declare the 'ctrl' variable 'AS 
Object'. But this is slower, as each symbol applied to 'ctrl' will need a 
lookup in the symbol table of the object class.

I hope I was clear, as it is difficult for me to explain that in English.

Note that is exactly like C++, with classes having virtual methods only. 
Except that C++ has no dynamic typing of course.

Regards,

-- 
Benoit Minisini




More information about the User mailing list