[Gambas-user] prb:Left() does not work correct.

Christopher Brian Jack brian at ...1334...
Mon Apr 24 00:05:26 CEST 2006


On Sun, 23 Apr 2006, ron wrote:

> gb2-1.9.28
>
> In the tag of some controls on my form is a string as
> "bind:fld=mykey"
>
> PUBLIC FUNCTION Init(parent AS Form) AS Boolean
> DIM idx AS Integer
> DIM frm AS Form
> DIM ctrl AS Control
>
>   frm = parent
>   FOR EACH ctrl IN frm.Children
>     PRINT ctrl.Tag 'shows correct tag message
>     PRINT Left(ctrl.tag, 4) 'gets the required text
>     IF Left(ctrl.tag, 4) = "bind" THEN ' <-- skips
>       checktag(ctrl)  ' <---never called
>     END IF
>   NEXT
> END
>
> PUBLIC FUNCTION Init(parent AS Form) AS Boolean
> DIM idx AS Integer
> DIM frm AS Form
> DIM ctrl AS Control
> DIM t AS String
>
>   frm = parent
>   FOR EACH ctrl IN frm.Children
>     PRINT ctrl.Tag
>     t = Left(ctrl.tag, 4)
>     PRINT t
>     IF t = "bind" THEN ' works
>       checktag(ctrl)  ' <-- is now done
>     END IF
>   NEXT
> END

> As far I know should Left(ctrl.tag, 4) return a string type.
> Change to Left$(ctrl.tag, 4) also did not work
> After a try with second code block that did work I try
> with Cstr(Left$(ctrl.tag, 4)) and it works.

I recall something in the documentation about the need to use CStr when
pulling text content off forms controls because they are stored using
UTF-8 and the type acts every other way like a string but doesn't work
correctly with functions like Mid Left or Right when used directly from
the control (eg: If someControl.text = "foo" Then ...does not hit...) then
If allows for Variant types and doesn't automatically convert as you were
hoping (it is a similar problem to the gotcha with division and having to
use CFloat in integer divisors if you want a Float result).  Using CStr
forms regular strings from UTF-8 encodings found on form control text
properties (including tags).

In the latter case you declare t as a regular String type and in the line
[t=someControl.tag] you implicitly force a CStr conversion because the
String class type of the form's tag property is being forced into
something that can only take the basic String type and not UTF-8 String
classes.

The basic String type doesn't like UTF-8 string types and trying to mix
the two, as you've noticed, can cause problems.

.=================================================.
|  Christopher BRIAN Jack aka "Gau of the Veldt"  |
+================================================='
| brian _AT_ brians-anime _DOT_ com
`=================================================-
Hi Spambots, my email address is sputnik at ...1334...




More information about the User mailing list