Equal or not equalEdit
In gambas the = sign is used differently than it is in mathematics. It is used to assign a value to a variable.
When the equal sign shows up in the code, you should always think of it as:
- It is assigned
- A place in computer memory is reserved for the variable
- Fill the memory with this value
Then you will not run into illogical conclusions.
The following code is correct in Gambas. Try it out . What is the result?
a = 5 a = a * 4
Translated to natural language this says:
Assign 5 to the variable a Multiply 4 with the old variable a and assign it to the new variable a.
Maybe the first person who started assignment in computers came from Arabia. Perhaps this is why we have to read the line from the right to left.
By the way: the result is 20.
How does this work in a little program: You need one command button to get the program going. The result is shown in the terminal window.
PUBLIC SUB Button1_Click() DIM a AS Integer a = 5 a = a * 4 PRINT "a = ";a END
A math teacher would not like these equations, but in gambas (or in basic in general) it is correct. Of course, you could make things clearer by using two different variables.
PUBLIC SUB Button1_Click() DIM a AS Integer DIM b AS integer a = 5 b = a * 4 PRINT b END
DIM a AS Integer
is not an assignment. It is a declaration of a variable as a datatype. In this case a is of the datatype integer.
Theory of the assignmentEdit
The following code shows you in general the method of assignment:
Variable = Expression
Assigns the value of an expression to one of the following elements. :
- A local variable.
- A function parameter.
- A global (class) variable.
- An array slot.
- An object public variable.
- An object property.
iVal = 1972 Name = "Gambas" hObject.Property = iVal cCollection[sKey] = Name