Excerpts from REALbasic By Matt Neuburg (for educational purposes only)

Variables and Constants

In REALBasic, as in many computer languages, the association of a name with a value is performed in code by means of the = operator. For an assignment the name goes on the left side of the = and the value goes on the right side.

lvalue = rvalue

for example:

Dim StudentName as String

StudentName = "Jennifer"

 

Datatypes and Declarations

Datatypes and Declaration of Variables

excerpt from

 

In REALbasic, every value is of some one type, called its datatype. For instance, a value might be a whole number - an integer. Or, it might be text - a string. Before a name can be associated with a value by means of assignment, there has to be a declaration stating the datatype for any value that will be assigned to that name. So, before making any of the assignments in the preceding examples, we would have to declare the names myAge, thisYear, and myBirthYear are all destined to receive integer values.

In REALbasic code, we perform this declaration by means of a Dim statement:

dim thisYear as integer

dim myBirthYeas as integer

dim myAge as integer

Declarations my be combined, like this:

Dim thisYear, myBirthYear, myAge as integer, myName as string

It is an error to use an undeclared variable name, and it is an error to assign to a variable a value whose datatype is not the datatype for which the variable was declared. Therefore, you must remember to declare every variable before you use it, and you mustn't violate a variable's datatype once you've declared it. In a nutshell: you must say what you're planning to do, and you can do only what you said.

 

Methods, Subroutines, Procedure and Functions

Methods, subroutines, procedures and functions all serve the same purpose. They contain a block of code that can be called (told to execute) by some other piece of code, or by REALbasic itself.

For example when the following code executes, a call to val() is made. Val() contains a chunk of code that performs the conversion of a String to an Integer. The following line calls val twice. The first call translates an editfield1.text to a number and the second call translates editfield2.text to a number.

Dim num as Integer

num = val(editfield1.text) + val(editfield2.text)

 

For the Datatype Exercise

Write down each statement and state whether it is correct or an error. If it is an error write down why.

dim myName as string

dim thisYear as integer

myName = "Ryan"

thisYear= 2008

myName = 2008

myName = thisYear

yourName = "Reader"