Defining Constant Expressions
Typically, you use constant expressions for both control flow (i.e. incrementing counters) and computational purposes. The format consists of a left-hand expression (variable) of a certain type, an assignment operator (=) and a constant of the matching type. Examples are:
iConstant = 1; rConstant = 2.27052;
The system evaluates constants at compile time so that you will know immediately whether they are correct.
Defining and Using Math Expressions
A powerful capability in Oasis montaj is to apply some mathematical operation to selected rows in one or more channels of data, such as in the following expression:
Z = 2.5*sqrt(X*X + Y*Y)
where X, Y and Z are the names of channels.
The GX Programming Language provides this same functionality with the math expression object EXP. To define the above math expression within a GX you could type:
ZExp=Create_EXP(Data,âÂÂZ=2.5*sqrt(X*X + Y*Y);âÂÂ,64);
Note the semicolon “;” terminating the expression. The number 64 represents the maximum length of the expression string. A call to the Math_DU function is used to apply the expression to a given line:
Math_DU(Data,Line,ZExp);
In many cases, however, channel and variable values may not be known ahead of time to the programmer, but instead are determined at run-time, perhaps by being selected by the user. In this case, the expression may be defined using replaceable parameters, indicated by pre-pending dollar-sign characters “$” to the local variable names:
GetString_SYS(âÂÂMYGXâÂÂ, âÂÂX_CHANâÂÂ, sXCh); GetString_SYS(âÂÂMYGXâÂÂ, âÂÂY_CHANâÂÂ, sYCh); GetString_SYS(âÂÂMYGXâÂÂ, âÂÂZ_CHANâÂÂ, sZCh); rMult = 2.5; ZExp = Create_EXP(Data,"$sZCh=$rMult*sqrt($sXCh*$sXCh+$sYCh*$sYCh));",350);
The 350 value at the end of the expression, giving the maximum expression length in characters, recognises the fact that individual channel names may each contain up to 64 characters (defined by DB_SYMB_NAME_SIZE in db.gxh) characters. (See the EXP.GXH header file for details).
Math expressions may also be applied to grids. The expression syntax is identical, and the procedure is similar, but it uses the IEXP object. (See the IEXP.GXH header file for details).
Casting Types
Arithmetic operations in GX Developer must remain true to type. Integer and real types can be mixed only if type casting is performed. The (real) and (int)operators are cast operators that convert the operand on the right to the specified type. Other programming languages (including C) will perform casting automatically, but this is a common source of programming errors. GXC requires that the GX programmer make all type casts explicit.
For example, the following operations will produce an error on compilation:
iVal = 10.0; // should be: iVal = 10; rVal = 0; // should be: rVal = 0.0; iVal = rVal + 1; // should be: iVal = (int)rVal + 1; rVal = iVal/rVal; // should be: rVal = (real)iVal/rVal;
Class handles and symbols can also be stored in an integer variable, which can be useful in some applications. However, to store the handle in an integer variable requires that the class be cast to an integer. When the class handle is used, it must be cast back to the type of the class. The following example illustrates this:
DB hDB; // working database int(10) iDB; // array of 10 database handles int i; string(GS_MAX_PATH) sName; { for (i=0;i<10;i++) { // --- code to get databases --- ... iDB[i] = (int)hDB; } // --- get the name of the 5âÂÂth database --- GetName_DB( (DB)iDB[4],DB_NAME_FILE,sName); ... }