Getting the long name(or ID) of a control
Richard Gaskin
ambassador at fourthworld.com
Wed Jan 27 11:21:02 EST 2016
Ralph DiMola wrote:
> Riddle me this Batman:
>
> I have 2 controls named "F1"(field) and "menu"(button) both in
> a group named G1
>
> Why does this work...????
> put "F1" into somevar
> put the long name of control somevar into someothervar
>
> But this does not work...
> put "F1 of group G1" into somevar
> put the long name of control somevar into someothervar
>
> This works....
> put "F1 of group G1" into somevar
> put the long name of ("control"&&somevar) into someothervar
>
> This also works....
> put "F1" into somevar
> put the long name of ("control"&&somevar) into someothervar
>
> But this does not
> put "menu of group G1" into somevar
> put the long name of ("control"&&somevar) into someothervar
>
> But this does
> put quote&"menu""e&&"of group G1" into somevar
> put the long name of ("control"&&somevar) into someothervar
>
> And this works
> put "menu" into somevar
> put the long name of control somevar into someothervar
>
> Menu seems to be a keyword or something. What am I missing? Should
> there always be quotes around control names? (banging head against
> wall)
"Menu" is indeed a keyword, but not a true object type. It's a
convenient alternative to "button" when you want to address the
*current* menu bar specifically. In most cases that'll resolve to an
object in the menu group of the current card, but if the card has no
menu group and a defaultMenubar has been named, it'll look for the
object in the defaultMenubar.
As for the rest, object references in LC are, IMO, somewhat smarter in
their resolution than in any other xTalk.
When mixing variables and literals in HC or SC, we often need to put the
entire thing into a string before passing it to the engine as an object
reference. But in LC we can mix and match object references much more
flexibly, provided we keep in mind how the parser works.
Specifically, (AFAIK) when using nested references (<something> of
<something else> in a variable, the contents of the variable must be an
object reference, that is, it must include the object type.
Applying this to the examples that didn't work:
put "F1 of group G1" into somevar
put the long name of control somevar into someothervar
In this case, adding the word "control" before "somevar" would allow the
parser to use that as a complete reference. Seems safe to do there,
since "control" is generic and useful for all object types (provided of
course you don't have objects of different types with the same name).
Here we have something else, two different object types used together:
put "menu of group G1" into somevar
put the long name of ("control"&&somevar) into someothervar
That concatenation produces:
control menu of group G1
...which as Scott noted won't work because "menu" is a keyword, somewhat
akin to writing:
control button of group G1
If quoted it would work, but since it's not capitalized I'm not sure if
it's actually the name of a menu object or a typo and something else was
intended.
As Jacque noted, it's very useful to adopt the habit of always quoting
literal strings like object names. It benchmarks roughly twice as fast
in my tests, and to my eye makes code easier to read.
An exception is when the object name is the only thing in a variable, e.g.:
put "File" into tObjName
put the long id of btn tObjName of grp "MBAR" into tObj
Variables that contain portions of an object reference that's non-nested
(in which the variable contains only the name, ID, or ordinal number)
can be used by themselves quite well.
In fact, I haven't found a way to force the interpreter to includes
quotes in such cases, e.g. this:
put "File" into tObjName
put the long id of btn (quote& tObjName "e) of grp "MBAR" into tObj
...throws an "object not found" error, which is understandable since the
parenthetical expression is treated as a whole string and there is no
object whose name includes the quotes.
HTH -
--
Richard Gaskin
Fourth World Systems
Software Design and Development for the Desktop, Mobile, and the Web
____________________________________________________________________
Ambassador at FourthWorld.com http://www.FourthWorld.com
More information about the use-livecode
mailing list