Translations: ArticleDevX in Portuguese

## Gambas provides familiarity to VB coders using Linux

"The phenomenal quantity of bugs and inconsistencies that makes Visual Basic so delightful persuaded me to start this project". That's how Benoit Minisini, a 30-year-old French man living in the suburbs of Paris starts describing his project, named Gambas.

Gambas is a VB replacement for Linux. Not a clone, since Benoit never liked Microsoft's implementation: "It seems that Microsoft is aware of the poor quality of its language, as VB.Net is not backward compatible with older versions of Visual Basic. I think they have thrown away the Visual Basic interpreter source code, and that VB .Net is just a .Net runtime compiler whose syntax looks like the Visual Basic one."

Thus, Gambas is intended to be a better Visual Basic.

Benoit took from VB that which he found really useful: the BASIC language and the easy IDE. He also took some ideas from Java, resulting an easy and powerful language. How powerful? The entire IDE was created in Gambas itself, an indicator of its level of sophistication even before reaching 1.0.

In this article we'll try to describe Gambas' current status, and how it compares with VB. We'll compare screenshots and source code, so you can get the entire picture.

### Background

Gambas is not the first attempt to create a VB replacement for Linux by a longshot, but is the closest to maturity.

In fact, there were many other unfinished attempts to create a VB clone for Linux: RapidQ bought out by RealBASIC, which is just now shipping a Linux runtime with no IDE, Phoenix BASIC is frozen for 3 years, KBasic stalled, GNOME Basic shifted its focus to provide VBA support for GNOME apps, XBasic is mature but more primitive than a VB, HBasic is a worthy contender but unstable.

On the other hand, there are many BASIC languages without integrated IDEs, and many IDEs without the BASIC language.

Gambas has an unique mix of features that will help thousands of VB developers to migrate from Windows to Linux. Therefore, Gambas can bring more applications and users to Linux.

Finally, Gambas' component design means someone should be able to write a GUI component based on GTK+ with the same interface than the QT one, so that every gambas program can then switch between QT GUI and GTK+ GUI.

### Basics of BASIC

One of the first programs you'll certainly find, in any programming language, is the venerable "Hello World".

#### Hello VB World

1. Start with a blank form
2. Place a Command Button (Command1) and a label (Label1) on the form
3. Write the code:

    Sub Command1_Click()
      Label1.Caption = "Hello World!"
    End Sub

#### Hello Gambas World

1. Start with a blank form
2. Place a Button (Button1) and a label (Label1) on the form
3. Write the code:

    SUB Button1_Click()
      Label1.Text = "Hello World!"
    END

See also: How do I... say Hello World in Gambas

If you don't want to create a form-based program, you can start a project without forms, and write the code:

    SUB main()
      PRINT "Hello World!"
    END

This is pretty much the simplest thing you can do with Gambas - and yet, something Visual Basic can't do! (In VB there is a "Print" method, but it only applies to the debugger and to GDI objects like printer, form or picturebox.)

### Differences from VB

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.

From the DifferencesFromVB topic:

#### 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:

VB | Gambas | Type of file
--- | --- | ---
.vbp | .project (just .project, one per directory) | Project definition file
.bas | .module | Module
.cls | .class | Class file
.frm | .form | Form definition file
.frx | (whatever you want) | Binary resource files

- 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.

#### VB Has It, Gambas Doesn't

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

- Function and procedure parameters are passed by value. 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.

- 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 (0.62) 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.

- The MouseMove event only occurs when a mouse button is depressed in Gambas, meaning you can't use it for the traditional mouse-over effects yet (other than the standard Tooltip associated with each control). The exception is the DrawingArea control, which has a Tracking property that allows getting mouse move events even if no mouse button is pressed.

#### Gambas Has It, VB 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.qt" component in Project Properties and make sure you define a Sub Main.

- 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 object, 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.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.

- In Gambas, you can create and display as many copies of a form as you wish.

- You can embed a Gambas form into another one: when you instantiate the first, specify the second as the parent.

- And of course, 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.

### Leveraging the power of Linux

"This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface." -- Doug McIlroy

One of the greatest advantages of Gambas over VB is that Gambas is built upon the Unix philosophy. That means you can leverage the power of Linux, and its thousands of tools, instead of writing code.

Let's see some examples.

- examples of controlling other programs using pipes
  - cd burner? ()
  - sample player? (mpg123?)
  - downloading files from the internet? (e.g., `lynx -source url` )

### Leveraging your current knowledge

If you ever programmed in BASIC, you'll have that warm feeling of familiarity when you start programming in Gambas.

VB programmers will feel comfortable right from the beginning.

For sure, Gambas is not VB, and VB programmers must be prepared for a few differences. But most changes are there for a specific reason: to make a better language. (It's important to remember that Gambas borrowed some good ideas from Java and other programming languages.)

With Gambas anyone can start programming graphical applications in minutes, and it will bring much more programmers and applications to GNU/Linux.

### Converting your legacy code

If you are a VB programmer and are unhappy with the recent platform changes (and the associated cost), you may consider converting your applications to Gambas.

Gambas developers have already released a small script, currently in version 0.1, which will convert VB forms to Gambas.

Vb2Gb was programmed in Perl due the strong text processing capabilities of that language, which soon will be implemented in Gambas.

In VB, form description and source code are mixed in a single File. Gambas splits these in two files: the form description and a class module.

There are also some control and property names that we can convert automatically. For instance, in VB we have "CommandButtons", while in Gambas (or QT?) they are called simply "Buttons"; the "Caption" property is called "Text" in Gambas.

### Summary

Gambas is the closest thing to VB in the Linux world. It wasn't primarly intended to replace the millions of lines of VB code, but provide a rapid development tool based on the BASIC language. Nonetheless, Gambas already provides a tool to ease the migration from legacy VB applications.

Gambas is in steady progress. It's current version is considered alpha software, because some features are still being defined, but Gambas is already being used in complex applications such as the IDE itself. There's no hurry to release 1.0, and the goal is to make things right from the first time.

Gambas has a roadmap. Some of the new features that are expected for the next releases:

- A Network component
- A SDL component
- An object persistence system
- A report designer
- A Perl-like regular expression component

According to Daniel Campos, creator of the Network component, it is stable enough to be called 'beta' version, and already provides some interesting functionality:

- DNS/NIS Client
- Socket Client : to start a TCP or UNIX socket connection.
- Socket Server : to serve on TCP or (UNIX, not yet) sockets.
- Datagram Client/Server : UDP sockets
- SerialPort : a class to manage serial devices (RS-232, etc...)

TO-DO: close the article with unbridled optimism