oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Programming REALbasic, Part 1: Syntax of the Language

by Wei-Meng Lee

Are you a programmer new to the Mac platform? Or maybe you are one of the millions of VB programmers (like me) who are well versed in the Windows platform and are looking at opportunities in the Mac world. And perhaps you don't have time to learn a new language such as Objective C, C++, or Java. If you fit any of these descriptions (and are trying to leverage your existing skills for Mac development), then with this article, I am going to show you how you can get started in Mac development using REALbasic.

If you are really impatient, I have an article on how to use the IDE included in the REALbasic package: "Building Mac Applications Using REALbasic 4.5 for Mac OS X." In this first article of a series, we shall learn the basic syntax of the language. I am going to assume that you have some programming experience and that you understand basic terms like variables, conditions, assignments, etc. (I won't be touching too much on Mac development in this article, but there is sufficient syntax for you to follow the examples in subsequent articles.)

Reserved Words

One of the first things to learn when programming in a new language is the list of reserved words used by the language. REALbasic has 81 reserved words. Here they are:

And Dim Function Me Raise To
Array Do GoTo Mod Redim TRUE
As Double Handles Module Rem Try
Boolean DownTo If Namespace Return Until
ByRef Each Implements New Select Wend
ByVal Else In Next Self While
Call ElseIf Inherits Nil Shared #bad
Case End Inline68k Not Single #else
Catch Event Integer Object Static #endif
Class Exception Interface Of Step #if
Color Exit Is Or String #pragma
Const FALSE IsA Private Sub
DebugPrint Finally Lib Protected Super
Declare For Loop Public Then

One bit of good news: REALbasic is not case sensitive. So Dim, dim, and diM all refer to the same thing.

Declaring Constants and Variables

Related Reading

By Matt Neuburg

You define constants in REALbasic using the const keyword:

const PI=3.14

To declare variables, use the Dim keyword:

Dim radius as single

You can also declare multiple variables in a single statement.

Dim radius, area as single

In the above example, both radius and area are single variables.

Variables' Scope

Generally, you can declare variables anywhere in your program. However, block scope variables are not allowed, as in the following example:

Dim i as Integer
For i = 1 to 10
  Dim j as integer  ' not allowed
  'j = i * i

Here, the declaration of j is not allowed within the For loop. To declare j, move it outside of the For loop.

Data Types

REALbasic supports the following intrinsic data types:

  • Integer: Whole numbers, such as 5, 20, -19, etc.
  • Single: Decimal values, such as 3.14, -2.45.
  • Double: Same as Single, except that it can store a larger range of numbers.
  • Boolean: True or False.
  • Color: A data type that defines a color.

These five data types are value-typed. Value-type variables store the value directly, as opposed to storing the reference to a value, as in Reference-type variables (more on this later). For example, suppose I have the following declaration and assignment:

Dim number1, number2 as integer

Then, changing the value of number1 has no effect on the value of number2:

msgbox str(number2) ' prints out 5

Besides the five intrinsic data types, REALbasic variables can also be of the types:

  • Variant: A container for any type of data.
  • Object: The base of all classes.


You can use either one or a combination of the following characters as the delimiter for comments: ', //, and REM.

' This is a comment
// This is also a comment
REM  This is yet another comment!

Mathematical Operators

REALbasic supports the following mathematical operators:

+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer Division
Mod Modulo

The following example illustrates their uses:

Dim ans as variant
Ans = 3+4        ' 7
Ans = 3 - 4      ' -1
Ans = 3*4        ' 12
Ans = 3/4        ' 0.75
msgbox (str(VarType(ans))) ' 5; means Single or Double
ans = 4\3        ' 1
msgbox (str(VarType(ans))) ' 2; means Integer
ans = 4 mod 3    ' 1
ans = 8 mod 4  ' 0

Notice that ans is declared as a variant. Its data type changes, depending on what is assigned during runtime. To check the data type of ans during runtime, use the VarType() function.


An array is simply a collection of data of the same type. Simply append a pair of parentheses to your variable to declare it as an array. Here are some examples:

Dim num1() as integer   ' dynamic size; use Redim during runtime
Dim num1(-1) as integer ' same as above
Dim num2(0) as integer ' only 1 element
Dim num3(4) as integer ' 5 elements; indices 0 to 4

Note that you cannot assign array values during declaration. The following is not allowed:

Dim num4() as integer = {1,2,3} ' not allowed
num3() = {1,2,3,4,5}            ' also not allowed

Also, arrays cannot be assigned directly, as in:

num1= num2  ' not allowed

You have to assign array values individually:


You can sort arrays using the Sort() method:


To verify that the array has been sorted, print it out:

Dim index as integer
For index = 0 to 4
  msgbox Str(num3(index))

Once an array has been declared, you can:

  • Resize it using ReDim.
  • Increase the size by one using the Append() method
  • Insert an item using the Insert() method.
  • Remove an item using the Remove() item.

The following example shows some array manipulations:

Dim str5(2) as string    ' array of 3 elements
str5(0) = "z"            ' assign value to each element
str5(1) = "B"
str5(2) = "a"
str5.append "b"          ' add one element to the array; str5(3)= "b"  
str5.insert 2, "g"       ' str(2) becomes "g", the rest is 
                         ' pushed downwards
str5.remove 2            ' remove str(2)
str5.sort                ' sort the array
For index = 0 to UBound(str5)
  msgbox (str5(index))
ReDim str5(9)            ' redimension str5 to 11 elements

Unlike VB.NET (or VB6), the ReDim statement automatically preserves the values of the previous elements. Of course, if the new array size is less than the original one, then you will lose some of the elements.

You can also create multi-dimensional arrays:

Dim str1(1,1) as string ' creates a 2 by 2 array of string
str1(0,0) = "REALbasic"
str1(0,1) = "is"
str1(1,0) = "easy"
str1(1,1) = "to use."

Pages: 1, 2

Next Pagearrow