The "." operator and if statements

I seem to have misunderstood something really basic with the DXL language.  Below I have provided the script and the error message i receive, I will also try to explain my intentions with the script

Module m = current
Object o = null
const string oAttr = "Attribute 1"
const string oStatus = "Status"
const string mandReq = "attr1"
 
for o in m do 
{
   if (o.oAttr == mandReq)
   {
       /* Do something useful */
   }
}
-E- DXL: <Line:9> incorrect arguments for (==)
-E- DXL: <Line:9> incorrect arguments for (if)
-I- DXL: All done. Errors reported: 2. Warnings reported: 0.

Each object in the module m has several attributes, two of them are  "Attribute 1" (string)  and "Status" (enumeration). If one object has the correct "Attribute 1" value i want to set the "Status" attribute value to something, for example "approved". But this does not work, why does the comparison fail? What does the "." operator actually do? I thought it was used to access "members" of the object o, i.e. its different attributes...

 
 

kip820 - Tue Jul 09 05:50:47 EDT 2013

Re: The "." operator and if statements
Bob_Swan - Tue Jul 09 06:21:32 EDT 2013

Try putting "" after the o.Attr,

thus

  if (o.oAttr "" == mandReq)

I spent ages finding that, and still fall over it when in a hurry.

Re: The "." operator and if statements
kip820 - Tue Jul 09 06:42:00 EDT 2013

Aaahhh, thank you for that! I have no idea why it works though, do you?

Re: The "." operator and if statements
HazelWoodcock - Tue Jul 09 07:13:02 EDT 2013

kip820 - Tue Jul 09 06:42:00 EDT 2013

Aaahhh, thank you for that! I have no idea why it works though, do you?

By concatenating the empty string onto the o.oAttr, you are forcing the result to be a string and comparing with a string so it all works properly.

 

Re: The "." operator and if statements
kip820 - Tue Jul 09 07:48:34 EDT 2013

HazelWoodcock - Tue Jul 09 07:13:02 EDT 2013

By concatenating the empty string onto the o.oAttr, you are forcing the result to be a string and comparing with a string so it all works properly.

 

So if I had compared with an integer this trick would not have worked? When I omitted the "" it did not work, was that because the interpreter saw a object type being compared with a string type? What is then the point of the "." operator?

Re: The "." operator and if statements
Tony_Goodman - Tue Jul 09 11:23:58 EDT 2013

kip820 - Tue Jul 09 07:48:34 EDT 2013

So if I had compared with an integer this trick would not have worked? When I omitted the "" it did not work, was that because the interpreter saw a object type being compared with a string type? What is then the point of the "." operator?

The o.attr notation returns an attribute reference.

Adding the empty string forces the  expression to be cast to a string.

To compare types other than strings you must assign the value to a variable and use the variable in the comparison.

e.g

int i = o.attr

if (i > 0) 

 

If you are assigning the string to a variable then you do not need to cast because the interpreter casts it for you.

e.g.

string s = o.attr

Re: The "." operator and if statements
llandale - Tue Jul 09 15:24:41 EDT 2013

Yes:    if (o.oAttr "" == mandReq)

[o.oAttr] is a variable ot type "Attr__" and is specifically NOT the value of the attribute for that object.  That variable type should not be used by us (for reasons you don't care).  [if (o.oAttr == mandReq)] is comparing an "Attr__" with a "string" and that is why you get the error.

You can force the retrieval of the value of the attribute for an object in two ways (that I can think of)

  • s = o.oAttr    // putting the "Attr__" on right side of the equal sign (by itself)
  • s = "[" o.oAttr "]"   //concatenate the "Attr__" with a string (in this case the string after it, "]"

-Louie

Re: The "." operator and if statements
kip820 - Wed Jul 10 07:07:24 EDT 2013

llandale - Tue Jul 09 15:24:41 EDT 2013

Yes:    if (o.oAttr "" == mandReq)

[o.oAttr] is a variable ot type "Attr__" and is specifically NOT the value of the attribute for that object.  That variable type should not be used by us (for reasons you don't care).  [if (o.oAttr == mandReq)] is comparing an "Attr__" with a "string" and that is why you get the error.

You can force the retrieval of the value of the attribute for an object in two ways (that I can think of)

  • s = o.oAttr    // putting the "Attr__" on right side of the equal sign (by itself)
  • s = "[" o.oAttr "]"   //concatenate the "Attr__" with a string (in this case the string after it, "]"

-Louie

Thanks for all the replies, I've learned a lot from all the answers :)