Wiki Home

Write Expression


Namespace: VFP
From VFP Help - Object.WriteExpression[cPropertyName, cExpression]

11/18/2005 - Steven Blake There is a dearth of information on the Write Expression method which may lead you to believe it is a no-brainer to implement. The first parameter is indeed straight forward - a character string which may be literal, stored in a memvar, returned by a method, etc. Working in conjunction with the AMEMBERS() method, you can pull a nice array of property names in string format and filter for those you wish to effect a new value.

    *!*   Load property names into an array
    lnCounter = AMEMBERS(laProperty, This, 0)

    FOR EACH lcPropertyName IN laProperty
        IF m.lcPropertyName = m.lcSomeFilter
            Object.WriteExpression[m.lcPropertyName, lcExpression]

        ENDIF
   ENDFOR
The second parameter is a string of the command line you would implement to arrive at the resulting value. Forming that parameter properly is akin to looking through a mirror to write your name. Looks more simple than it is.

First rule is that the entire expression must be enclosed in quote marks. Second rule is any literal string data within the expression must be embedded between a second pair of marks.

lcPropName = "Greeting"
lcJustHello = "Hi there everyone"
*!* Square brackets are the second pair of marks
object.WriteExpression(m.lcPropName, "[" + m.lcJustHello + "]")
*!* Literal string requires second pair of marks
object.WriteExpression(m.lcPropName, "[Hi there everyone]")
Without the brackets in the above examples, the assignment silently fails (no error) and the object.Greeting property becomes a logical data type with the value .F.

If your code expression has no literal string data, then only one outside pair of quote marks is required. Even though memvars must be assigned as strings to form the expression, they may not be treated as such during evaluation. Also note local memvars are within scope for the evaluation.

lcRandomNumber = "2345"
lcAnniversay = "2005/01/11"
*!* Evaluated and assigned as number
object.WriteExpression(m.lcPropName, m.lcRandomNumber)
*!* Evaluated and assigned as string
object.WriteExpression(m.lcPropName, "STR(m.lcRandomNumber)")
*!* Evaluated and assigned as date
object.WriteExpression(m.lcPropName, "{^" + m.lcAnniversary + "}")
*!* Evaluated and assigned as date
object.WriteExpression(m.lcPropName, "CTOD(m.lcAnniversary)")
In the above STR() and CTOD() functions, the memvar is a string type but is handled by the function without quote marks. Here's a rewrite to show literal string handling for those functions.
*!* Evaluated and assigned as string
object.WriteExpression(m.lcPropName, "STR([" + m.lcRandomNumber + "])")
*!* Evaluated and assigned as date
object.WriteExpression(m.lcPropName, "CTOD([" + m.lcAnniversary + "])")


Contributors: Steven Blake
Category VFP Methods
( Topic last updated: 2005.11.18 01:58:56 PM )