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:
Operator | Description |
---|---|
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.
The complex or composite assignment operators represent only abbreviated forms of assignments with special operators in the expression:
Complex assignment operator | Description |
---|---|
Target += Expression | Synonym for the assignment: Target = Target + Expression |
Target -= Expression | Synonym for the assignment: Target = Target - Expression |
Target ? = Expression | Synonym for the assignment: Target = Target? Expression |
Target /= Expression | Synonym for the assignment: Target = Target / Expression |
Target \= Expression | Synonym for the assignment: Target = Target DIV Expression |
Target %= Expression | Synonym for the assignment: Target = Target MOD Expression |
Target &= Expression | Synonym for the assignment with the operator & to connect strings: Target = Destination & Expression |
Target &/= Expression | Synonym 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:
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).
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()