Verschillen met Visual Basic
Hoewel Gambas niet bedoeld is als clone voor Microsoft Visual Basic, is het wel een BASIC en er zijn vele gelijkenissen, en zelfs overeenkomsten met bepaalde mogelijkheden.
Misschien zijn er zelfs meer gelijkenissen dan verschillen, maar je kan niet zomaar een VB project kopieren naar Gambas en het daar compileren.
Het symbool %vb% zal soms opduiken op een pagina in het document om aan te geven dat er nota's beschikbaar zijn over hoe een bepaald kenmerk verschilt van VB, om de programmeurs te helpen die naar Gambas overschakelen.
Niet-taalgebonden verschillen
-
VB steekt de class code voor ieder form object in hetzelfde bestand als de form definition. Gambas houdt ze uit elkaar, in een .form en een a .class bestand.
-
File extention of bestandsuitgangen:
Bestandstype
|
Visual Basic
|
Gambas
|
Project definitie bestand
|
.vbp
|
.project (één per map)
|
Module
|
.bas
|
.module
|
Class bestand
|
.cls
|
.class
|
Form definitie bestand
|
.frm
|
.form
|
Binair "resource" bestand
|
.frx
|
Alle andere bestanden in de project map.
|
-
Gambas projects zijn gedefinieerd als een map met een .project bestand erin en alle andere bestanden in dezelfde directory. VB kan meer project-bestanden in één map hebben, en kan een bepaald broncodebestand uit een andere map gebruiken, in verschillende projecten, wat zo zijn voor en nadelen heeft.
-
Scherm-afmetingen worden in VB in "twips", gedaan, "units of 1/1440 of an inch"; in Gambas worden pixels gebruikt als eenheid.
-
Form controls zijn in Gambas standaard "private". Je kan dat veranderen in de Project Eigenschappen-dialoog (Project Properties) en de vakjes aanvinken bij Make form controls public .
-
Str$, Val, CStr... omzettingen gedragen zich verschillend. Zo gebruikt Gambas bv bij Str$ en Val de "localization settings", Visual Basic niet. Lees aandachtig de documentatie voor meer details. Merk op dat het Gambas gedrag logischer is :-).
Visual Basic wel, Gambas niet
-
Je kan geen code wijzigen in "Break mode" in Gambas; je moet het uitvoeren van het programma eerst stoppen.
-
In Gambas worden eenvoudige datatypes (integer, string, etc.) als waarde doorgegeven naar procedures and functies. Ze kunnen niet doorgegeven worden als referentie zoals in Visual Basic. Merk op dat VB parameters doorgeeft "by reference" als je niet het sleutelwoord ByVal gebruikt, wees dus voorzichtig als je een VB project probeert over te zetten. Maar de inhoud van object datatypes (array types, collections, objects) worden altijd doorgegeven als referentie, in beide talen!
-
Er is geen zichtbare variabele op projectniveau "project-wide global" in Gambas. Je kan dat omzeilen met een class die je Global noemt and zet je globale variabelen en "static public variables" in die class. Je kan ze dan in je project gebruiken als Global.variablename. Het is misschien geen sterk programmeerwerk, maar ze zullen tenminste herkend worden als globale variabelen telkens als je ze gebruikt ;-)
-
Unless you include
Option Explicit
in a VB module, you don't need to declare variables prior to using them. Gambas behaves as if Option Explicit
were always turned on, which makes for much better code at the expense of a bit more work.
-
There's no direct Gambas equivalent to the Index of VB form controls. You can easily create arrays of controls, but you have to do it in code. There's currently no way to do it graphically. Thus, when you copy a control and paste it back on the form it came from, rather than prompting you to create a control array it automatically renames the copied control to an appropriate name.
-
You can't currently create transparent labels in Gambas; the background is always opaque. This is now possible in the development version.
-
The MouseMove only occurs when a mouse button is depressed in Gambas. The exception is the DrawingArea control, which has a Tracking that allows getting mouse move events even if no mouse button is pressed.
-
In VB you could put two strings together with the symbol
+
. Because the +
sign is only used for mathematical addition in Gambas, you should use & instead, when you want to add one string to another.
-
The colon
:
does not work to seperate your code. You must take a new line instead.
-
The print command in VB 3.0 did not make a Linefeed. If you used it to print out some text with printer.print, then text got lost. The Print Command in Gambas puts everything in one line. There is nothing lost.
-
In VB you can use Mid$() as an instruction to cut out a substring and put in some other. In Gambas, you can not use it to assign a new substring to it. For example, in VB:
MyString = "The dog jumps": Mid$(MyString, 5, 3) = "fox"
results in MyString = "The fox jumps"
. That does not work in Gambas. You must do things like that: MyString = Left$(MyString, 4) & "fox" & Mid$(MyString, 8)
.
-
Non-ASCII characters which can be legal for use in identifiers in VB code, are not acceptable in Gambas.
-
Thankfully, in Gambas you cannot use GOTO to trap errors! Instead, use CATCH, FINALLY or TRY.
-
ENUM cannot be used to enumerate integer constants. Instead you have to define each ENUM element as a constant.
Examples
CONST ADDITION AS Integer = 1
CONST SUBSTRACTION AS Integer = 2
Gambas heeft het, Visual Basic niet.
-
Unlike VB, you're not required to compile in GUI support if you want to write a Gambas command-line application. Just unselect the gb.qt4 component in Project Properties and make sure you define a SUB Main().
-
Instead of the
WITH EVENTS
keyword, you must provide an " name" to objects that raise events. See the documentation of NEW for more information.
-
Gambas has the concept of control groups, which allow you to handle events from any number of different controls with one handler subroutine. This reduces redundant code and can be used to do many of the things VB's control indexes can do, and some things that VB can't.
-
Whereas VB makes it impossible to run a program synchronously and receive its output without learning how to do API calls (Shell merely launches the program in the background), Gambas allows you to do so using SHELL and EXEC, control the processes you start using the Process class, and even read from and write to them, allowing you to easily add functionality with helper applications. This makes it incredibly easy to write Gambas front-ends for almost any command-line procedure.
-
You can do all of the above with Unix devices and special files as well, such as serial or parallel ports. Use the
/proc
filesystem to write a RAID monitor, for example, or use named pipes to get multiple channels of information from a back-end program in any other language.
-
To make an odd-shaped window you just set the ME.Picture and ME.Mask of the current window to a picture that has transparent areas. VB requires API calls and a bit more work.
-
You can create controls and menu dynamically, just by instantiating them with the NEW instruction.
-
You can embed a Gambas form into another one: when you instantiate the first, specify the second as the parent.
-
Controls have Enter and Leave events, which allow you to know when the mouse pointer enters a control and when it leaves it. You can use them to easily implement mouse-over effects.
-
You can read data in binary files and automatically manage the endianness of its format, by using the ByteOrder of the Stream class.
-
Gambas uses charset internally, and so projects are fully and easily internationalizable.
-
Gambas is Free Software whose development environment is written in itself, allowing you to customize it to a large degree using just your BASIC skillset.
And many many other things... Just add them as you like! :-)
Zelfde functionaliteit. Andere Terminologie
-
End Sub=/=End Function
: see END.
-
Exit Sub=/=Exit Function
: see RETURN. Also, rather than setting a with the same name as the function and then exiting the function, you can simply include the desired return value as a parameter to RETURN.
-
End
(end program): see QUIT.
-
Arrays use brackets instead braces. So use
DIM x[9] AS
instead DIM x(9) AS Float
-
Arrays do not have the extra element for indexing as 1..n, index must always be 0..(n-1)
-
On Error Goto
: see TRY, CATCH and FINALLY.
-
Msgbox
: see Message. Normally you'd want Message.info.
-
VB's default
InputBox
function (pop up a dialog prompting for a value which is returned to the calling program) has no direct equivalent in Gambas yet, but see the InputBox page for a class you can download and include in your projects to serve the same purpose (and more). The development version has an InputBox .
-
DoEvents
: see WAIT. WAIT also replaces the frequently used Windows API "sleep" function.
-
Inserting Double Quotes in Strings: Rather than two consecutive double quotes as in VB, use the backslash as an escape character as in C or Perl (").
-
VScrollBar, HScrollBar: Gambas' ScrollBar replaces both of these. It automatically figures out whether you want a vertical or horizontal scrollbar based on the control's dimensions: make it wide, and you get a horizontal scrollbar; make it tall, and get a vertical one.
-
Open and Save dialogs: You can use either the qt or enhanced KDE dialogs in place of the Windows common dialog. Some of the properties are named differently and filters are specified with a String array, like this:
[ "Filter 1 (*.foo)" , "Filter 2 (*.bar)" ]
. The syntax has changed in the development version.
-
Validating text entry: In VB, certain events have a Cancel parameter you can set to prevent the from being handled normally after your handler is done, so that for instance you can allow only letters or numbers to be typed, perform field validation, or force entry to be all upper or lower case. In Gambas, this is done by using the STOP EVENT instruction.
-
Keyboard and mouse handlers do not take parameters. They use instead static public members of the Mouse and Key classes. For example:
-
In Gambas, the Timer routine returns the number of elapsed seconds since the program start. In VB, it returns the number of elapsed seconds since midnight.
-
Do not use the Form.Load . It is a completely different from the Visual Basic Load instruction. In Gambas, it is just a static that creates the implicit form instance.