Gambas Documentation - /doc/diffvb

/doc/diffvb

# Differences From Visual Basic

While Gambas is not intended to be a clone of Microsoft Visual Basic, it's still BASIC and there are many similarities between the two languages, and many one-to-one relationships between features.

There are probably more similarities than differences, but you can't simply copy your VB projects over and expect them to compile under Gambas.

The symbol %vb% will sometimes appear on a page in this documentation to indicate that there are notes available on how using the feature described on that page differs from accomplishing the same task in VB, to help those programmers who may be migrating to Gambas from that language.

## Non-Language-Specific Differences

- VB embeds the class code for each form object into the same file as the form definition. Gambas keeps them separate, in a .form and a .class file.

- File extensions:

Type of file | Visual Basic | Gambas
--- | --- | ---
Project definition file | .vbp | .project (just .project, one per directory)
Module | .bas | .module
Class file | .cls | .class
Form definition file | .frm | .form
Binary resource files | .frx | Any other file stored in the project directory.

- Gambas projects are defined as a directory with a .project file in it, and all of the files in that directory. VB can have multiple project files in each directory and can pull the same source file from a different directory into different projects, which has its benefits and disadvantages.

- Screen measurements in VB are done in "twips", units of 1/1440 of an inch; in Gambas they're done in actual pixels.

- Form controls in Gambas programs are private by default. You can change this by going into the Project Properties dialog and checking the Make form controls public checkbox.

- Str$, Val, CStr... conversion functions behave differently. For example, Str$ and Val use the localization settings in Gambas, whereas they don't in Visual Basic. Read the documentation carefully for more details. Note that Gambas behavior is more logical :-).

## Visual Basic Has It, Gambas Doesn't

- You can't edit code in Break mode in Gambas; you need to end program execution first.

- In Gambas, simple datatypes (integer, string, etc.) are passed by value to procedures and functions. They cannot be passed by reference as in Visual Basic. Note that VB passes parameters by reference if you do not use its ByVal keyword, so be careful when you try to port a VB project. Also, the contents of object datatypes (array types, collections, objects) are always passed by reference in both languages!

Passing arguments by reference is now possible in the development version.

- There is no such thing as a project-wide global variable in Gambas. As a workaround, consider making a class called `Global` and declaring your global variables as static public variables in that class, and then referring to them as `Global.VariableName` in your project. It's still poor programming practice but at least they'll be identified as global variables whenever you use them ;)

- 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 property 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 event only occurs when a mouse button is depressed in Gambas. The exception is the DrawingArea control, which has a Tracking property 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 separate 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:
  `in VB` MyString = "The dog jumps" MID(MyString, 5, 3) = "fox" PRINT MyString ---- The fox jumps

That does not work in Gambas. You must do things like that:

    'in Gmabas
    MyString = "The dog jumps"
    MyString = LEFT(MyString, 4) & "fox" & MID(MyString, 8)
    PRINT MyString

    The fox jumps

This Mid$() syntax is now possible in the development version.

- 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 Has It, Visual Basic Doesn't

- 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 "event 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` file system 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 property 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 property of the Stream class.

- Gambas uses UTF-8 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 skill set.

And many many other things... Just add them as you like! :-)

## Same Functionality, Different Terminology

- `End Sub=/=End Function`: see END.

- `Exit Sub=/=Exit Function`: see RETURN. Also, rather than setting a variable 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 method.

- `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 event 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 event handlers do not take parameters. They use instead static public members of the Mouse and Key classes. For example:

  - Mouse.X and Mouse.Y for the mouse coordinates.

  - Mouse.Left to know if the left button is pressed.

  - Key.Code to get the code of a key.

  - Key.Shift to know if the SHIFT key is pressed.

  - ...and so on.

- 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 method. It is a completely different method from the Visual Basic Load instruction. In Gambas, it is just a static method that creates the implicit form instance.