User Tools

Site Tools


19.8.2 Class Expression

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.

.EnvironmentCollectionIn the collection Expression.Environment the values for the undefined symbols in the expression are stored or these values are read from the collection.
.TextStringIn Expression.Text the expression is stored as text in a string. This expression can contain almost all operators and subroutines of Gambas.
.ValueVariantExpression.Value returns the computed value of the expression.

Table 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:

  • First, two variables are created that generate a new Expression object at runtime and a Collection that can later be assigned to the Expression property Expression.Environment.
  • If the expression contains undefined symbols, values are assigned to the undefined symbols and these values are stored in a variable of the data type 'Collection'.
  • Then the expression from which the value is to be calculated is specified.
  • Then the Expression.Environment property is assigned the filled Collection.
  • Finally, the calculated value of the expression stored in the Expression.Value property can be used further.

The source code consistently implements the above concept:

' Gambas class file
Public Sub Form_Open()
    FMain.Resizable = False
    vlbParameterA.Type = vlbParameterA.Number
    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
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
Public Sub btnComputeY_Click()
  vlbFunktionswert.Value = Compute(txbExpression.Text, vlbParameterA.Value, vlbParameterB.Value, vlbArgumentX.Value)
Public Sub btnClose_Click()


Figure 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
    Message.Error(Error.Where & gb.NewLine & "Fehlertext: " & Error.Text)
     Return fFunktionswertY

This will give you the following error message for the expression 'a*sin(x)+cos(b*x)+c', for example:

Symbol 1

Figure Error message 1

and for the expression 'a/(sin(x-b))' with parameter value b=2 and argument x=2 this message:

Symbol 2

Figure Error message 2


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k19/k19.8/k19.8.2/start.txt · Last modified: 21.10.2023 by emma

Page Tools