* There is no global variables in Gambas!
As a workaround, put them in your main module and declare them as PUBLIC. If you do not have a main module in your project, but a main form, then declare them as STATIC PUBLIC. To access these variables, you must use the name of the main module or form:
MyMainModule.MyGlobalVariable or
MyMainForm.MyGlobalVariable.
* To know if a string is empty, it is not necessary to use the Len() function. You can directly test it, as an empty string is FALSE and a non-empty string is TRUE.
For example, instead of doing :
IF Len(MyString) > 0 THEN ...
IF Len(MyString) = 0 THEN ...
You should do :
IF MyString THEN ...
IF NOT MyString THEN ...
* The LAST keyword returns the last control that has received an event. This is very useful when you want to write an event handler that is independent of any control name. For example, let's suppose you want to write a calculator program. You have defined ten buttons, one for each digit, each one in the same group "Digit". The Tag of each control is set to the digit drawn in the button. Your event handler may look like that :
PUBLIC SUB Digit_Click()
Display = Display & LAST.Tag
RefreshDisplay
END
* String functions Left$, Right$ and Mid$ have useful behaviors in Gambas. The second parameter of Left$ and Right$ is optional, and is one by default.
Left$("Gambas") returns "G"
Right$("Gambas") returns "s"
This second parameter can be negative, and then gives the number of characters not to extract.
Left$("Gambas", -2) returns "Gamb"
Right$("Gambas", -2) returns "mbas"
Likewise, the third argument of Mid$ can be negative, and then gives the number of characters from the end of the string not to extract.
Mid$("Gambas", 2, -2) returns "amb"
* You can use the DEBUG instruction to print debugging messages to the console (namely the standard error output). It behaves exactly like the PRINT instruction. These messages are prefixed with the class name, method name and line number of the DEBUG instruction. The debugging messages are automatically removed when creating an executable without debugging information.
* Error management in Gambas is done with the following instructions:
TRY tries to execute a statement without raising an error. The
ERROR instruction is used just after to know if the statement was executed correctly.
TRY MyFile = OPEN "/etc/password" FOR WRITE
IF ERROR THEN PRINT "I cannot do what I want!"
* Error management in Gambas:
CATCH indicates the beginning of the error management part of a function or procedure. It is put at the end of the function code. The catch part is executed when an error is raised between the beginning of the function execution and its end. If an error is raised during the execution of the catch part, it is normally propagated.
SUB ProcessFile(FileName AS STRING)
...
OPEN FileName FOR READ AS #hFile
...
CLOSE #hFile
CATCH ' Executed only if there is an error
PRINT "Cannot process file "; FileName
END
FINALLY introduces the code executed at the end of the function, even if an error was raised during its execution. The finally part is not mandatory. If there is a catch part in the function, the finally part must precede it. If an error is raised during the execution of the finally part, it is normally propagated.
SUB ProcessFile(FileName AS STRING)
...
OPEN FileName FOR READ AS #hFile
...
FINALLY ' Always executed, even if a error is raised
CLOSE #hFile
CATCH ' Executed only if there is an error
PRINT "Cannot print file "; FileName
END
* - Current Directory
> If you have to start using things like 'EXEC [ "pwd" ] TO sPwd' to get
> the current dir it doesn't make sense to use a Gambas script.
>
There is no current directory in Gambas!
--
BenoƮt Minisini