cat • assign

Assignments

[ LET ] Destination = Expression

Assigns the value of an expression to one of the following elements:

  • A local variable.

  • A function parameter.

  • A global or a class variable.

  • An array slot.

  • An object public variable.

  • An object property.

This cannot be used to set the value returned by a function. To assign the value of a function, use the RETURN statement.

Some instructions that return something may use the assignment syntax as well: EXEC, NEW, OPEN, RAISE, SHELL.

Examples

iVal = 1972
Name = "Gambas"
hObject.Property = iVal
cCollection[sKey] = Name
...

Assignments of embedded structures or arrays

Since 3.17

If the assignment Destination is actually a structure or an embedded array, then the contents of Expression is recursively copied in the Destination.

Of course, Expression is first converted to the datatype of Destination before the copy is done.

When an array is copied, if the source and the destination do not have the same number of elements, then the smallest number of elements is used to do the copy.

The copy is done only when the structure or the array are embedded. If not embedded, then structures and arrays are normal Gambas objects, and the assignment is just a reference copy.

Examples

Struct Person
  FirstName As String
  LastName As String
  Age As Integer
  BirthDate As Date
  Height As Float
End Struct

Private $aPeople[16] As Struct Person
Private $hFirst As Struct Person
Private $aOther[4] As Struct Person

...

' Copy the 4th people into $hFirst
$hFirst = $aPeople[3]
' Copy the four first people into $aOther
$aOther = $aPeople
' Only the four first elements of $aPeople are modified
$aPeople = $aOther

See also