Variables.
Today, we'll try to explain how to use variables in Gambas 3.
A) What is "variable"
Variable is used to do some actions whatever its type.
Example :
Variable with integer type or float type will be use for mathematic operations.
Text variable could be used to manipulate characters chain.
B) Déclaration of variable.
To declare variable, it's very important to ask to to you Why and for who it will be used.3 cases :
To declare variable who will be used inside all current Form, Write this declaration on the top of code Form out of all methods as bellow :
'Gambas class
Private myvariable as integer
To declare variable who will be used by all Form in your application, Put this declaration Public as bellow:
Public myvariable as integer
Finaly, to declare variable inside method (Public monbouton_click() for example), Use Dim command as bellow :
Public mybutton_click()
Dim myvariable as integer
End Sub
This variable will be seen only inside this method.
To have good code and for easy maintenance, It's very important to use as soon as possible only Private variable or variables inside method only.
You will ask me immediatly this question :
But how to exchange variables between methods?
You need just to give in parameter one variable and return result by this command : RETURN
One Simple Example:
I send to one method one integer variable and i want to return one text.
If i send 0, i want to return "Good morning"
If i send 1, i want to return "Good evening"
I write my method as bellow:
Private Sub whenisit(myvariable as integer) as string
select case myvariable
case 0
Return "Good morning"
Case 1
Return "Good evening"
End Select
Let explain this simple code :
Private Sub whenisit is private declaration used only in my Form
(myvariable as integer) I declare one variable who will be used inside my method.
As String inform me Return will return one variable with type String.
And to finish, i call this method somewhere else inside my programm as bellow:
Dim mytexte as string
mytexte = whenisit(0)
Information: If you don't put any variable between these () symbol,
gambas will return you an error message and say it miss one parameter.
Last information, If you want to use one public variable in all your application, declare one public variable inside module as bellow
In one module mymodule, Write this:
Public myvariable as string
And in all your program, you can read/write inside variable :
mymodule.myvariable = "I modified my variable"
Thanks for reading.