This class represents a dynamic Gambas expression. In this context, dynamic refers to the possibility of specifying one's own symbols in the expression and assigning values to these undefined symbols only at runtime of the Gambas programme.
The number of properties of the Expression class is quite manageable. Besides the three properties mentioned below, it has only one method Compile, which you will mostly use indirectly.
Expression | Type | Description |
---|---|---|
.Environment | Collection | In the collection Expression.Environment the values for the undefined symbols in the expression are stored or these values are read from the collection. |
.Text | String | In Expression.Text the expression is stored as text in a string. This expression can contain almost all operators and subroutines of Gambas. |
.Value | Variant | Expression.Value returns the computed value of the expression. |
Table 19.8.2.1: Properties of the Expression class
The following project shows how to use the Expression class to evaluate an expression. The following concept has proved successful:
The source code consistently implements the above concept:
' Gambas class file Public Sub Form_Open() FMain.Center() FMain.Resizable = False vlbParameterA.Type = vlbParameterA.Number vlbParameterA.SetFocus() vlbParameterB.Type = vlbParameterB.Number vlbArgumentX.Type = vlbArgumentX.Number vlbFunktionswert.Type = vlbFunktionswert.Number vlbFunktionswert.ReadOnly = True '-- Initialisierung: txbExpression.Text = "a*sin(x)+cos(b*x)" '-- Default-Expression vlbParameterA.Value = +0.125 vlbParameterB.Value = -2.5 vlbArgumentX.Value = 0.525 End Private Function Compute(sExpression As String, fParameterA As Float, fParameterB As Float, fArgumentX As Float) As Float Dim fFunktionswertY As Float Dim cEnvironment As New Collection Dim myExpression As New Expression ' cEnvironment.Add(fParameterA, "a") '-- ALTERNATIVE ' cEnvironment.Add(fParameterB, "b") ' cEnvironment.Add(fArgumentX, "x") cEnvironment["a"] = fParameterA '-- Dem Symbol a wird der Wert vlbParameterA.Value zugewiesen cEnvironment["b"] = fParameterB '-- Dem Symbol b wird der Wert vlbParameterB.Value zugewiesen cEnvironment["x"] = fArgumentX '-- Dem Symbol x wird der Wert vlbArgumentX.Value zugewiesen myExpression.Environment = cEnvironment myExpression.Text = txbExpression.Text fFunktionswertY = myExpression.Value Return fFunktionswertY End Public Sub btnComputeY_Click() vlbFunktionswert.Value = Compute(txbExpression.Text, vlbParameterA.Value, vlbParameterB.Value, vlbArgumentX.Value) End Public Sub btnClose_Click() FMain.Close() End
Figure 19.8.2.1: Calculation of the function value (function coulter)
Have you noticed that this project is quite dynamic? You can change not only the expression, but also the values of the two parameters of the function chart f(a,b,x) and the argument x. You implement simple error handling if you add the following lines to the source code:
... fFunktionswertY = myExpression.Value Return fFunktionswertY ...
with these lines:
TRY fFunktionswertY = myExpression.Value If ERROR Then Message.Error(Error.Where & gb.NewLine & "Fehlertext: " & Error.Text) Else Return fFunktionswertY Endif
This will give you the following error message for the expression 'a*sin(x)+cos(b*x)+c', for example:
Figure 19.8.2.2: Error message 1
and for the expression 'a/(sin(x-b))' with parameter value b=2 and argument x=2 this message:
Figure 19.8.2.3: Error message 2
Chapter & Project
Download