<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - Differences From Visual Basic
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../doc+en">Up</a>&nbsp;
<a href="../doc+en">Previous</a>&nbsp;
<a href="dragndrop+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
Differences From Visual Basic
</h1>
While <a href="../def/gambas+en">Gambas</a> 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.
<p>
There are probably more similarities than differences, but you can't simply copy your VB projects over and expect them to compile under <a href="../def/gambas+en">Gambas</a>.
<p>
The symbol <img border="0" src="../../img/vb.png" align="center"> 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 <a href="../def/gambas+en">Gambas</a> from that language.
<p>
<h2>Non-Language-Specific Differences</h2>
<p>
<ul>
<li>VB embeds the class code for each form object into the same file as the form definition. <a href="../def/gambas+en">Gambas</a> keeps them separate, in a .form and a .class file.
<p>
<li><a href="../comp/gb/file+en">File</a> extensions:
</ul>

<table class="table" border="0" bordercolor="#000000" cellpadding="4" cellspacing="0">
<tr><th>
Type of file
</th><th>
Visual Basic
</th><th>
<a href="../def/gambas+en">Gambas</a>
</th></tr>
<tr class="dark"><td valign="top">
Project definition file
</td><td valign="top">
.vbp
</td><td valign="top">
.project (just .project, one per directory)
</td></tr>
<tr><td valign="top">
Module
</td><td valign="top">
.bas
</td><td valign="top">
.module
</td></tr>
<tr class="dark"><td valign="top">
<a href="../comp/gb/class+en">Class</a> file
</td><td valign="top">
.cls
</td><td valign="top">
.class
</td></tr>
<tr><td valign="top">
<a href="../comp/gb.qt/form+en">Form</a> definition file
</td><td valign="top">
.frm
</td><td valign="top">
.form
</td></tr>
<tr class="dark"><td valign="top">
Binary resource files
</td><td valign="top">
.frx
</td><td valign="top">
Any other file stored in the project directory.
</td></tr>
</table>
<p>
<ul>
<li><a href="../def/gambas+en">Gambas</a> 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.
<p>
<li><a href="../comp/gb.sdl.image/screen+en">Screen</a> measurements in VB are done in &quot;twips&quot;, units of 1/1440 of an inch; in <a href="../def/gambas+en">Gambas</a> they're done in actual pixels.
<p>
<li><a href="../comp/gb.qt/form+en">Form</a> controls in <a href="../def/gambas+en">Gambas</a> programs are private by default. You can change this by going into the Project Properties dialog and checking the <i>Make form controls public</i> checkbox.
<p>
<li><a href="../lang/str+en">Str$</a>, <a href="../lang/val+en">Val</a>, <a href="../lang/cstr+en">CStr</a>... conversion functions behave differently. For example, <a href="../lang/str+en">Str$</a> and <a href="../lang/val+en">Val</a> use the localization settings in <a href="../def/gambas+en">Gambas</a>, whereas they don't in Visual Basic. Read the documentation carefully for more details. Note that <a href="../def/gambas+en">Gambas</a> behaviour is more logical :-).
<p>
</ul>

<h2>Visual Basic Has It, <a href="../def/gambas+en">Gambas</a> Doesn't</h2>
<p>
<ul>
<li>You can't edit code in Break mode in <a href="../def/gambas+en">Gambas</a>; you need to end program execution first.
<p>
<li>In <a href="../def/gambas+en">Gambas</a>, 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!
<p>
<li>There is no such thing as a project-wide global variable in <a href="../def/gambas+en">Gambas</a>. 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 ;)
<p>
<li>Unless you include <tt>Option Explicit</tt> in a VB module, you don't need to declare variables prior to using them. <a href="../def/gambas+en">Gambas</a> behaves as if <tt>Option Explicit</tt> were always turned on, which makes for much better code at the expense of a bit more work.
<p>
<li>There's no direct <a href="../def/gambas+en">Gambas</a> equivalent to the <a href="../comp/gb.db/index+en">Index</a> 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.
<p>
<li>You can't currently create transparent labels in <a href="../def/gambas+en">Gambas</a>; the background is always opaque.
<p>
<li>The MouseMove event only occurs when a mouse button is depressed in <a href="../def/gambas+en">Gambas</a>. The exception is the <a href="../comp/gb.qt/drawingarea+en">DrawingArea</a> control, which has a Tracking property that allows getting mouse move events even if no mouse button is pressed.
<p>
<li>In VB you could put two strings together with the symbol <tt>+</tt> . Because the <tt>+</tt> sign is only used for mathematical addition in <a href="../def/gambas+en">Gambas</a>, you should use & instead, when you want to add one string to another.
<p>
<li>The colon <tt>:</tt> does not work to seperate your code. You must take a new line instead.
<p>
<li>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 <a href="../def/gambas+en">Gambas</a> puts everything in one line. There is nothing lost.
<p>
<li>In VB you can use <a href="../lang/mid+en">Mid$</a>() as an instruction to cut out a substring and put in some other. In <a href="../def/gambas+en">Gambas</a>, you can not use it to assign a new substring to it. For example, in VB: <tt>MyString = &quot;The dog jumps&quot;: <a href="../lang/mid+en">Mid$</a>(MyString, 5, 3) = &quot;fox&quot;</tt> results in <tt>MyString = &quot;The fox jumps&quot;</tt>. That does not work in <a href="../def/gambas+en">Gambas</a>. You must do things like that: <tt>MyString = <a href="../lang/left+en">Left$</a>(MyString, 4) & &quot;fox&quot; & <a href="../lang/mid+en">Mid$</a>(MyString, 8)</tt>.
<p>
<li>Non-ASCII characters which can be legal for use in identifiers in VB code, are not acceptable in <a href="../def/gambas+en">Gambas</a>.
<p>
<li>Thankfully, in <a href="../def/gambas+en">Gambas</a> you cannot use <a href="../lang/goto+en">GOTO</a> to trap errors! Instead, use <a href="../lang/catch+en">CATCH</a>, <a href="../lang/finally+en">FINALLY</a> or <a href="../lang/try+en">TRY</a>.
<p>
<li>ENUM cannot be used to enumerate integer constants. Instead you have to define each ENUM element as a constant.
</ul>

<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example"> CONST ADDITION AS Integer = 1
 CONST SUBSTRACTION AS Integer = 2</pre>
<h2><a href="../def/gambas+en">Gambas</a> Has It, Visual Basic Doesn't</h2>
<p>
<ul>
<li>Unlike VB, you're not required to compile in GUI support if you want to write a <a href="../def/gambas+en">Gambas</a> command-line application. Just unselect the <a href="../comp/gb/gb+en">gb</a>.qt component in Project Properties and make sure you define a <a href="../lang/sub+en">SUB</a> Main().
<p>
<li><a href="../def/gambas+en">Gambas</a> 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.
<p>
<li>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), <a href="../def/gambas+en">Gambas</a> allows you to do so using <a href="../lang/shell+en">SHELL</a> and <a href="../lang/exec+en">EXEC</a>, control the processes you start using the <a href="../comp/gb/process+en">Process</a> 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 <a href="../def/gambas+en">Gambas</a> front-ends for almost any command-line procedure.
<p>
<li>You can do all of the above with Unix devices and special files as well, such as serial or parallel ports. Use the <tt>/proc</tt> 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.
<p>
<li>To make an odd-shaped window you just set the <a href="../lang/me+en">ME</a>.<a href="../comp/gb.qt/window/picture+en">Picture</a> and <a href="../lang/me+en">ME</a>.<a href="../comp/gb.qt/window/mask+en">Mask</a> property of the current window to a picture that has transparent areas. VB requires API calls and a bit more work.
<p>
<li>You can create controls and menu dynamically, just by instantiating them with the <a href="../lang/new+en">NEW</a> instruction.
<p>
<li>You can embed a <a href="../def/gambas+en">Gambas</a> form into another one: when you instantiate the first, specify the second as the parent.
<p>
<li>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.
<p>
<li>You can read data in binary files and automatically manage the endianness of its format, by using the <a href="../comp/gb/stream/byteorder+en">ByteOrder</a> property of the <a href="../comp/gb/stream+en">Stream</a> class.
<p>
<li><a href="../def/gambas+en">Gambas</a> uses <a href="../def/utf8+en">UTF-8</a> charset internally, and so projects are fully and easily internationalizable.
<p>
<li><a href="../def/gambas+en">Gambas</a> is <a href="../lang/free+en">Free</a> Software whose development environment is written in itself, allowing you to customize it to a large degree using just your BASIC skillset.
<p>
</ul>

And many many other things... Just add them as you like! :-)
<p>
<h2>Same Functionality, Different Terminology</h2>
<p>
<ul>
<li><tt>End Sub</tt>/<tt>End Function</tt>: see <a href="../lang/end+en">END</a>.
<p>
<li><tt>Exit Sub</tt>/<tt>Exit Function</tt>: see <a href="../lang/return+en">RETURN</a>. 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 <a href="../lang/return+en">RETURN</a>.
<p>
<li><tt>End</tt> (end program): see <a href="../lang/quit+en">QUIT</a>.
<p>
<li>Arrays use brackets instead braces. So use <tt><a href="../lang/dim+en">DIM</a> x[9] <a href="../lang/as+en">AS</a> <a href="../lang/type/float+en">Float</a></tt> instead <tt>DIM x(9) AS Float</tt>
<p>
<li>Arrays do not have the extra element for indexing as 1..n, index must always be 0..(n-1)
<p>
<li><tt>On <a href="../comp/gb/error+en">Error</a> Goto</tt>: see <a href="../lang/try+en">TRY</a>, <a href="../lang/catch+en">CATCH</a> and <a href="../lang/finally+en">FINALLY</a>.
<p>
<li><tt>Msgbox</tt>: see <a href="../comp/gb.qt/message+en">Message</a>. Normally you'd want <a href="../comp/gb.qt/message+en">Message</a>.<a href="../comp/gb.info/info+en">Info</a>.
<p>
<li>VB's default <tt>InputBox</tt> function (pop up a dialog prompting for a value which is returned to the calling program) has no direct equivalent in <a href="../def/gambas+en">Gambas</a> yet, but see the InputBox page for a class you can download and include in your projects to serve the same purpose (and more.)
<p>
<li><tt>DoEvents</tt>: see <a href="../lang/wait+en">WAIT</a>. <a href="../lang/wait+en">WAIT</a> also replaces the frequently used <a href="../comp/gb.qt/windows+en">Windows</a> API &quot;sleep&quot; function.
<p>
<li>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 (").
<p>
<li>VScrollBar, HScrollBar: <a href="../def/gambas+en">Gambas</a>' <a href="../comp/gb.qt/scrollbar+en">ScrollBar</a> 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.
<p>
<li>Open and Save dialogs: You can use either the Qt or enhanced KDE dialogs in place of the <a href="../comp/gb.qt/windows+en">Windows</a> common dialog. Some of the properties are named differently and filters are specified with a <a href="../comp/gb/string+en">String</a> array, like this: <tt>[ &quot;Filter 1 (*.foo)&quot; , &quot;Filter 2 (*.bar)&quot; ]</tt>
<p>
<li>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 <a href="../def/gambas+en">Gambas</a>, this is done by using the <a href="../lang/stopevent+en">STOP EVENT</a> instruction.
<p>
<li>Keyboard and <a href="../comp/gb.qt/mouse+en">Mouse</a> event handlers does not take parameters. They use instead static public members of the <a href="../comp/gb.qt/mouse+en">Mouse</a> and <a href="../comp/gb.qt/key+en">Key</a> classes. For example:
<ul>
<li><a href="../comp/gb.qt/mouse+en">Mouse</a>.X and <a href="../comp/gb.qt/mouse+en">Mouse</a>.Y for the mouse coordinates.
<li><a href="../comp/gb.qt/mouse+en">Mouse</a>.Left to know if the left button is pressed.
<li><a href="../comp/gb.qt/key+en">Key</a>.Code to get the code of a key.
<li><a href="../comp/gb.qt/key+en">Key</a>.Shift to know if the SHIFT key is pressed.
<li>...and so on.
<p>
</ul>
<li>In <a href="../def/gambas+en">Gambas</a>, the <a href="../lang/timer+en">Timer</a>() routine returns the number of elapsed seconds since the program start. In VB, it returns the number of elapsed seconds since midnight.
<p>
<li>Do not use the <a href="../comp/gb.qt/form+en">Form</a>.<a href="../comp/gb.qt/form/load+en">Load</a> method. It is a completely different method from the Visual Basic Load instruction. In <a href="../def/gambas+en">Gambas</a>, it is just a static method that creates the implicit form instance.
</ul>

</div>
</body>
</html>

