User Tools

Site Tools


k8:k8.1:start

8.1 Assignment operators

In the Gambas language, the assignment operator is the equals sign =. The syntax for an assignment operation can generally be described by Target = Expression. The value of Expression can be assigned to one of the following targets:

  • local variable,
  • Function parameters,
  • global variable or class variable,
  • Array,
  • Object public variable,
  • Object property.
OperatorDescription
Target == Printout Direct, simple assignment

Table 8.1.1: Assignment operator

The assignment operation is subject to legal associativity. This means that the printout on the right-hand side is evaluated first and then assigned to the target on the left-hand side.

Please note that the equal sign bivalent is also used as a comparison operator in the following sections of chapter 8.

8.1.1 Complex assignment operators

The complex or composite assignment operators represent only abbreviated forms of assignments with special operators in the expression:

Complex assignment operatorDescription
Target += ExpressionSynonym for the assignment: Target = Target + Expression
Target -= ExpressionSynonym for the assignment: Target = Target - Expression
Target ? = ExpressionSynonym for the assignment: Target = Target? Expression
Target /= ExpressionSynonym for the assignment: Target = Target / Expression
Target \= ExpressionSynonym for the assignment: Target = Target DIV Expression
Target %= ExpressionSynonym for the assignment: Target = Target MOD Expression
Target &= ExpressionSynonym for the assignment with the operator & to connect strings: Target = Destination & Expression
Target &/= ExpressionSynonym for the assignment with the operator & to connect strings for paths: Target = Target &/ Expression

Table 8.1.1.1: Complex assignment operators

Example:

[1]   Dim fNumber As Float
[2]
[3]   fNumber = 9.55
[4]   fNumber += 2 * (-3.21) ^ 3  ' Error: Print fNumber += 2*(-3.21)^3
[5] ' fNumber = fNumber + 2 * (-3.21) ^ 3
[6]   Print fNumber

Comments:

  • The equals sign in line 3 is the (simple) assignment operator.
  • In the 4th line the summand 2- (-3,21)^3 is added to the value of fNumber and the variable fNumber is assigned as a new value.
  • The assignment fNumber = fNumber + 2- (-3,21)^3 does the same thing
  • Attention: The statement PRINT fNumber += 2- (-3,21)^3 returns a syntax error.

Examples:

[1] PUBLIC CONST DefaultStartX AS Float = -5.0
[2] Private aMatrix As Variant[]
[3] aMatrix = New Variant[]
[4] Dim aNames As String[] = ["Hans", "Maria", "Peter", "Anna", "Robert"]
[5] TableView1.Sorted = NOT TableView1.Sorted
[6] TableView1.Columns.Count = 4
[7] TableView1.Header = TableView1.Both
[8] TableView1.Mode = Select.Single
[9] Me.Flags[Editor.ShowLimits] = (iLimit > 0) AND (iLimit < 3)
[10] btnTableViewExportCSV.Enabled = True
[11] If Row MOD 2 = 0 Then TableView1.Data.Background = Color.RGB(224, 224, 224) ' hellgrau

Please note: In line 11, the first equals sign = is a comparison operator, while the second equals sign = an assignment operator. The background color of the component TableView (target) is set to light grey (value of expression).

8.1.2 Special cases

Some statements that return values such as SWAP, EXEC, SHELL, OPEN, NEW, RETURN, or RAISE often also use the assignment syntax, as the following examples show:

SWAP:

IF Printer.Portrait = TRUE THEN SWAP MargeB, MargeL
SWAP aIndexField[iRow].Field, aIndexField[iRow - 1].Field
SWAP iForeground, iBackground

SHELL and EXEC:

hWhichAsProcess = EXEC ["which", "gnuplot"] Wait For Read
hGnuPlot = SHELL "gnuplot" For Read Write As "hGnuplot"

OPEN:

hDatei = OPEN (User.Home &/ "V24T" &/ "v24T.conf") FOR CREATE
hFile = OPEN sDTDDateiPfad FOR CREATE ' DTD file is newly created or emptied
fFile = OPEN Dialog.Path FOR READ  ' Open data file for reading
TRY fFile = OPEN Dialog.Path FOR WRITE ' Open data file for writing

NEW

$EditGrid = NEW TextBox(hParent) AS "EditGrid"
MyParentObserver = NEW Observer(Me.Parent) As "Parent"
IF NOT $cSlot.Exist(sSlot) THEN $cSlot[sSlot] = NEW Collection
hMenuItem = NEW Menu(hContext) As "mnuFileOpen"

RETURN - Return of a function value

RETURN (Value - Me.MinValue) / (Me.MaxValue - Me.MinValue)
RETURN Potentiometer.Spin
IF hCtrl.Window.TopLevel THEN RETURN Object.Type(hCtrl.Window)
RETURN Sin(x) * Cos(x / 0.56) + (Pi / 3)
RETURN FALSE

RAISE

' Gambas class file
' © 2008 Daniel Fuchs

EXPORT
CREATE PRIVATE
INHERITS UserControl
' The following events are already contained in the UserControl class:
EVENT Activate
EVENT MouseWheel() AS Boolean

...

PUBLIC SUB DrawingArea_MouseWheel()
  DIM Cancelled AS Boolean

  MySlideTimer.Stop
  Cancelled = RAISE MouseWheel()
  IF Cancelled OR NOT ME.Enabled THEN RETURN
  ME.Value = (ME.Value - Mouse.Delta * ME.GetSpin() * ME.PageStep)
  RAISE Activate

END ' DrawingArea_MouseWheel()
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.
k8/k8.1/start.txt · Last modified: 07.02.2022 (external edit)

Page Tools