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

Benoit Minisini gambas at ...1...
Mon Apr 24 00:14:11 CEST 2006


On Monday 24 April 2006 00:05, Christopher Brian Jack wrote:
> 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...
>

No, Ron's code should work in both cases. 

There is nothing related to UTF-8 in this problem. UTF-8 is an encoding 
method, it is not a datatype! The interpreter does not care what encoding is 
used inside a string. It is just a string for it.

Maybe there is a bug in variant/string conversion. I made some tests there, 
and everything worked correctly. I will try a test more similar to your code 
then...

Regards,

-- 
Benoit Minisini





More information about the User mailing list