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()
Pushbutton1.enabled=false
' or
Me.enabled=false
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=dict
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:
| = | Equal |
| < | 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
day="Monday"
else
if dayofWeek=2 then
day="Tuesday"
else
if dayofWeek=3 then
day="Wednesday"
else
if dayofWeek=4 then
day="Thursday"
else
if dayofWeek=5 then
day="Friday"
else
if dayofWeek=6 then
day="Saturday"
else
if dayofWeek=7 then
day="Sunday"
else
day="Error"
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
day="Monday"
elseif dayofWeek=2 then
day="Tuesday"
elseif dayofWeek=3 then
day="Wednesday"
elseif dayofWeek=4 then
day="Thursday"
elseif dayofWeek=5 then
day="Friday"
elseif dayofWeek=6 then
day="Saturday"
elseif dayofWeek=7 then
day="Sunday"
else
day="Error"
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"
else
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
Next
For i = 9 downTo 1 ' in descending order
' do something
Next
'---While-Wend
While (i<=9)
' do something
Wend
'---Do-Loop Until
Do
' do something
Loop Until(i>9)
'---Do-Until Loop
Do Until (i=10)
' do something
Loop
Summary
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) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 4 of 4.
-
Yes MORE please!
2006-06-10 13:20:15 KennethConrad [Reply | View]
Yes! more tutorials like this on RB on O'Reilly is really good! Matt's book is excellent but for a beginner it might get a bit too much into deeper philosophy.
Also I am VERY anxious to see a complete video tutorial on RB!
-
Ditto the above!
2003-11-07 02:43:10 anonymous2 [Reply | View]
Just another note appreciating the attention on RB< looking forward to more.
-
Excellent Intro.
2003-10-23 18:36:30 anonymous2 [Reply | View]
I think this is an excellent intro to RB. I hope that MacDevCenter continues to do articles on RB. RB is to Mac what VB is to PC, and is an important tool in the development tool box. I know that many Mac users are very snobby about RB and constantly assert that if you use RB and don't use Cocoa, you are not a real program and are not making real software. I have seen this attitude on many, many development sites, even popular ones. I am glad to see MacDev support RB.
Keep up the articles!
Paul






How can we compare a string witch must contain a string?
Example: Mystr 'operator contain' "value"
Thanks very much,
babe