oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Programming REALbasic, Part 1: Syntax of the Language
Pages: 1, 2

Functions and Subroutines

REALbasic supports both functions and subroutines. A function returns a value, while a subroutine does not.

Function AlsoDoSomething(i as Integer) as Double
   ' blocks of code
End function

Sub doSomething(ByVal i as Integer, ByRef j as Single)
   ' blocks of code
End sub

Both subroutines and functions can take input parameters. By default, arguments are passed in by value (ByVal). You can also pass in arguments by reference (ByRef).

The following shows how to call the subroutines and functions above:

Dim result as double
result = alsodosomething(5)
alsodosomething(5)               ' not allowed; must use return value 
result = alsodosomething()       ' not allowed; must supply arguments

Dim num as single
doSomething (5, 4)               ' not allowed; second argument must
                                 ' be a variable
doSomething (5, num)

Using Objects

REALbasic is an event-driven programming language that uses the object-oriented programming paradigm. You will use many objects in your REALbasic programming. Some examples of objects are the controls on your window, such as the PushButton control, PopupMenu control, etc. I won't go into the basics of object-oriented programming at this point, but I will assume that you are already familiar with basic OO concepts such as properties, methods, etc.

I will highlight two special keywords in REALbasic that are worth knowing: self and Me.

The self keyword is a reference to an object's parent object. As an example, suppose you want to set the title of your window to Windows 1. The following two statements are equivalent:

Window1.title = "Window 1"
   ' or
self.title = "Window 1"

The Me keyword is a reference to an event handler's control. Suppose you are writing the event handler that is fired when a PushButton control is clicked. The following two statements are equivalent:

Sub Action()
   ' or
End Sub

Objects are reference-type variables. That is, their values are not stored directly. Instead, they point to another storage location. As an example, consider the following example, where I have declared a Dictionary object (an object that contains a list of key-value pairs):

Dim dict as Dictionary

To instantiate the Dictionary object, I need to use the new keyword:

dict = new Dictionary

Note that you cannot combine the declaration and instantiation, as you commonly do in VB.NET:

Dim dict as new Dictionary   ' Not allowed!

You can use the Dictionary object like so:

dict.value("Name") = "Wei-Meng Lee"
dict.value("ID") = "WML"
dict.value(0) = 2345

When I try to assign the dict object to another (dict2) and make changes to dict2:

dim dict2 as dictionary
dict2.value("Name") = "Wei-Meng"

The changes in dict2 are reflected in dict:

msgbox dict.value("Name")  ' prints out Wei-Meng

Making Decisions

REALbasic supports the usual decision-making constructs, such the If-Then-Else construct:

Dim dayofWeek as integer
Dim day as string
dayofWeek = 3

if i<1 or i>7 then
  msgbox "Invalid value for i"
end if 
if i>=1 and i<=7 then
  msgbox "Valid value for i"
end if
if day<>"error" then
  msgbox "No error"
end if

The comparison operators are:

<Less than
<=Less then or equal to
>Greater than
>=Greater than or equal to
<>Not equal to

The logical operators supported are AND, OR, and NOT.

The If-Then-Else construct can also be nested, as the following shows:

if dayofWeek=1 then
  if dayofWeek=2 then
    if dayofWeek=3 then
      if dayofWeek=4 then
        if dayofWeek=5 then
          if dayofWeek=6 then
            if dayofWeek=7 then
            end if
          end if
        end if
      end if
    end if
  end if
end if

However, you can rewrite the above code with the elseif keyword, which makes it much shorter:

if dayofWeek=1 then
elseif dayofWeek=2 then
elseif dayofWeek=3 then
elseif dayofWeek=4 then
elseif dayofWeek=5 then
elseif dayofWeek=6 then
elseif dayofWeek=7 then
end if

A better way is to use the Select-Case construct:

select case dayofweek
case 1
  day ="Monday"
case 2
  day ="Tuesday"
case 3
  day ="Wednesday"
case 4
  day ="Thursday"
case 5
  day ="Friday"
case 6
  day ="Saturday"
case 7
  day ="Sunday"
  day = "error"
end select

Looping Constructs

Finally, REALbasic supports a number of looping constructs. The following examples are self-explanatory:

Dim i as integer
'---For loop
For i = 0 to 9 step 1  ' default is step 1
  ' do something

For i = 9 downTo 1     ' in descending order
  ' do something
While (i<=9)
  ' do something
'---Do-Loop Until
  ' do something
Loop Until(i>9)

'---Do-Until Loop
Do Until (i=10)
  ' do something


I hope you have a better feel of the REALbasic language after reading this article. With this foundation, you will be ready to dive deep into the world of REALbasic programming! In the next few articles, I will show you how you can develop Mac applications that you have always drooled at!

Wei-Meng Lee (Microsoft MVP) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies.

Return to the Mac DevCenter