PDA

View Full Version : Compilation Error


bmadigan
10-17-2004, 12:05 PM
Here is the error:

Syntax error: the token "Of" was not expected at this position.


This is the lines of code associated:

<statictext>Choose Paitent Type</statictext>
<radiogroup x="40" name="patientType">
<radiobutton value="New">New Patient</radiobutton>
<radiobutton value="Existing">Existing Patient</radiobutton>
<radiobutton value="Out Of State">Out Of State Patient</radiobutton>
</radiogroup>

Error Refers to the 'Out Of State' radiobutton line.

I'm at a total loss. We can't have the word 'of' anywhere? If I take it out, I don't get the compilation error any longer.

metasarah
10-17-2004, 12:51 PM
By default, attributes are evaluated as expressions, which is the reason for the compiler error. "Out Of State" is not a valid Javascript expression :|

The value attribute in basevaluecomponent (which radiobutton inherits from) does not declare a type. When I wrote it, I was thinking that people typically use number as values and it would provide the flexibility to use any object as a value. Only recently was it pointed out to me that the API is incovenient if you want to use a string.

The workaround is to double-quote your string, "'Out of State'"

Sarah

bmadigan
10-17-2004, 05:56 PM
Thank you for the response Sarah.

Normally I would use an integer value and through my Business Logic Layer associate the int value with a string, etc... I was just trying to pass a form value and echo out to another Tab (Results Tab) just for 'learning' purposes.

It's good to know, at least now I know where I can look in the future.

Just out of curiosity however, if renamed it to:

<radiobutton value="OutState">Out State Patient</radiobutton>

It worked fine. I guess you can take out the space(s) in the string without double quotes, but if you want a space in the string, you must use double quotes?

ptw
10-18-2004, 04:53 AM
When you say "worked fine", do you mean "did not give a compiler warning"?

Using OutState as your value will not give a compiler warning because OutState looks to the compiler like a variable, which is a legal Javascript expression. But, unless you define OutState somewhere and give it a value, you will find that the value of your radiobutton is undefined, not the string 'OutState' as you intended.

If you want to pass a String as a value in a value attribute whose type is (by default) expression, you must quote the string.

This is just one of those fiddly bits where a human can often infer that only a string makes sense or not, but a compiler, being just a machine, has to be told explicitly that a string is meant. The only two ways to tell the compiler is to either declare the type of the attribute to be String or to use a Javascript string literal (by surrounding the string with quotes).

[Conversely, if you have an attribute whose type has been declared as String and you find you want to pass an expression instead (i.e., compute the string) you can use the ${} syntax to override the automatic quoting implied by the String type declaration.]

bmadigan
10-18-2004, 05:41 AM
Exactly, it did not give me any compilation errors. You are also correct that the value remained 'undefined'.

Wow, I just wanted to test echoing a string, I guess I should have just used a textbox, I didn't think a radiobutton would have been a problem.

ows
10-18-2004, 08:13 AM
[Nothing in here that isn't in Tucker's message. I was writing it while he was posting. I'm leaving it here in case it's useful anyway.]

Yes, this is a very confusing error message.

<radiobutton id="mybutton" value="Out of State"> has the same effect as the JavaScript expression
mybutton.value = Out of State;

(I gave the radiobutton a name, mybutton, so that I could refer to it in script.)

This is a syntax error, because Out of State isn't a JavaScript expression.

<radiobutton id="mybutton" value="OutState"> has the same effect as
mybutton.value = OutState;

OutState happens to be a perfectly good JavaScript identifier, so this compiles fine. It will create an application that initializes mybutton.value to the value of the variable OutState --- probably undefined, but if you're not running in debug mode you won't see any error at all.

What you want is <radiobutton value="'Out of State'"> (that is, 'Out of State' in single quotes, inside the double quotes that enclose the attribute value). This is the same as
mybutton.value = 'Out of State';

The value attribute of <radiobutton> is treated as an expression because it has the type 'expression'. (The reference manual page on radiobutton refers you to the page on basevaluecomponents, which says this.) If it had type 'string' then your examples would behave as you expect without the quotes that make a string into a JavaScript expression. The title attribute of <window>, and the text attribute of <radiobutton>, have type 'string', so they don't need to be quoted.

Why isn't a <radiobutton> value a string, instead of an expression? The designers of the <radiobutton> class must have thought that it was more common to initialize the value from an expression such as <radiobutton value="f()">, <radiobutton value="myglobal">, or <radiobutton value="myglobal+1"> than to initialize it from a string.

ptw
10-18-2004, 08:24 AM
Well I don't think the radiobutton is a problem. You just have to wrap your mind around the fact that some attributes (e.g., name) are string-valued so you can say name="frobozz", but other attributes are general expressions so you have to give a valid Javascript expression inside the ". So, where in Javascript you might say:

var value = 'Out of State';

in LZX you would say:

... value="'Out of State'" ...

The outer " is required by XML, all attribute values must be quoted; the inner ' is telling the compiler that Out of State is a String literal.

As Sarah pointed out, the value attribute of radiobutton is intentionally an expression, because that is the most likely use. For other attributes where a String is most likely, they will have been declared as type="string", so you won't have to quote them (but you would have to unquote with attribute="${expression}" to use an expression.)