Topic
7 replies Latest Post - ‏2013-07-10T11:07:24Z by kip820
kip820
kip820
10 Posts
ACCEPTED ANSWER

Pinned topic The "." operator and if statements

‏2013-07-09T09:50:47Z |

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...

 
 
Updated on 2013-07-09T09:51:21Z at 2013-07-09T09:51:21Z by kip820
  • Bob_Swan
    Bob_Swan
    31 Posts
    ACCEPTED ANSWER

    Re: The "." operator and if statements

    ‏2013-07-09T10:21:32Z  in response to kip820

    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.

  • kip820
    kip820
    10 Posts
    ACCEPTED ANSWER

    Re: The "." operator and if statements

    ‏2013-07-09T10:42:00Z  in response to kip820

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

    • HazelWoodcock
      HazelWoodcock
      25 Posts
      ACCEPTED ANSWER

      Re: The "." operator and if statements

      ‏2013-07-09T11:13:02Z  in response to kip820

      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.

       

      • kip820
        kip820
        10 Posts
        ACCEPTED ANSWER

        Re: The "." operator and if statements

        ‏2013-07-09T11:48:34Z  in response to HazelWoodcock

        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?

        • Tony_Goodman
          Tony_Goodman
          270 Posts
          ACCEPTED ANSWER

          Re: The "." operator and if statements

          ‏2013-07-09T15:23:58Z  in response to kip820

          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

  • llandale
    llandale
    2943 Posts
    ACCEPTED ANSWER

    Re: The "." operator and if statements

    ‏2013-07-09T19:24:41Z  in response to kip820

    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

    • kip820
      kip820
      10 Posts
      ACCEPTED ANSWER

      Re: The "." operator and if statements

      ‏2013-07-10T11:07:24Z  in response to llandale

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