- Creating Behavior Without Programming
- Using the Code Editor to Write REALbasic Code
- Getting Help with the REALbasic Language
- Mastering Dim and Assignment Statements
- Making Tests and Comparisons
- Writing Code that Branches
- Writing Code that Repeats
- Writing Your Own Methods
- Extending the HTML Editor
- Creating the Indent Menu Item
- Removing Existing Indentation
- Inserting Indentation Before Tags
- Handling the Indent Level
- Extending the Project
Mastering Dim and Assignment Statements
The code that you write in a REALbasic handler or method is like a recipe: It's a sequence of instructions to be carried out in order. Many of these instructions manipulate data, and the assignment statement is the primary way to manipulate data in REALbasic programs. You met the assignment statement in Chapter 3; here, you'll see how to use it with specific types of data and with variables and properties.
Before a variable can be used, it must have a data type assigned to it. You do this with the Dim statement.
To create a variable:
Use the Dim statement:
Dim myVar as string Dim lengthMyVar as integer Dim i,j,k as integer
Tip
Any Dim statements in a handler must appear first, before any other statements in the handler (Figure 4.21).
Figure 4.21 Any Dim statements in a handler must appear before any other statements.
To create a variable that is an object: BL
Use the Dim statement:
Dim c as clipboard
Now you can access the properties of this object:
me.text = c.text
Variables can be created as simple data types such as integer, string, and Boolean, as shown earlier in this section; or as specific object types, such as windows, list boxes, and the Clipboard. They can also be created as containers for more than one value at a time. A variable that contains several values of the same type is called an array.
To create an array:
Use the Dim statement, specifying the dimension of the array:
Dim customer (100) as string
This statement sets aside space for 100 strings, each of which can be referred to by its index:
customer(0) = "Charlie Adams" customer(99) = "D. Zanuck"
Arrays are zero-based: Their indexes start at 0 and go to 1 less than the dimension in the Dim statement. So the preceding two strings are the first and last in the customer array.
To create a multidimensional array:
Use the Dim statement, and supply two or more dimensions:
Dim Chessboard (8,8) as string
This code creates an 8 x 8 array of values whose type is string. You could refer to a cell in this array this way:
Chessboard(0,0) = WhiteQueensRook
After you have created a variable, you can use it.
To assign a string value to a variable:
Place the variable on the left side of the equals sign (=) and the value it is to get on the right side (Figure 4.22):
Figure 4.22 Assigning a string value to a string variable.
Dim Mother, Father, Child (20) as string Mother = "Barb" Father = "Earl" Child(1) = "Doc" Child(2) = "Mark" Child(3) = "Ginny"
You can also use this form of the assignment statement, which makes clearer what is being assigned to what:
Let Mother = "Barb"
To assign a string to a property:
Whatever appears on one side of the equals sign in an assignment must be the same type as what appears on the other side. So both of these statements work
window.title = "Dorothy's Dog" window.title = someStringVariable
because the window.title property is of type string.
To assign a property's value to a string variable:
Assuming that someStringVariable has been created as a string variable, someStringVariable = window.title will put the window's title in someStringVariable.
Tip
You can never put a quoted string on the left side of an assignment statement. It's easy to see why. Unlike variables and properties, a string such as "Hello Dolly" is not a container to put a value in; it is a value. In general, you can't place a value to the left of an assignment statement.
To concatenate strings:
Use the plus-sign operator (+) to combine two strings:
Dim FullName, FirstName, LastName as →string FullName = FirstName + LastName
You can combine any number of strings in this way. To make the preceding example work, you probably want to put a space between the first and last names (Figure 4.23):
FullName = FirstName + " " + LastName
Figure 4.23 Concatenation: building up long strings from short ones.
To determine the length of a string:
One of the methods that REALbasic supplies for working with strings is len. len is a function, which means that it returns a value to you. When you give it a string, len returns an integer that is the number of characters in the string:
Dim s as string, ls as integer s = window1.title ls = len(s)
Tip
Functions return values. Anywhere in your code that a value of a particular data type is appropriate, a call to a function that returns that data type is appropriate. If i is of integer type, you can assign an integer value to it:
i = 47
You can also assign the value of a function that returns an integer value to it:
i = len(s1).
To extract part of a string:
REALbasic supplies three methods for extracting part of a string: left, right, and mid. You give left a string and n, the number of characters you want, and the method returns the first n characters of the string. Similarly, right returns the last n characters. mid takes three parameters: the string, the starting position in the string, and the number of characters to grab, counting from that position. So the following will put "The" in L, "Beagle" in R, and "Voyage" in M (Figure 4.24):
Figure 4.24 Using string functions to extract part of a string.
Dim S, L, R, M S = "The Voyage of the Beagle" L = left(S,3) R = right(S,6) M = mid(S,5,6)
Another string method, instr, can be used to get the location of a substring in a string. instr takes the string and the substring as parameters and returns an integer that is the position of the beginning of the first occurrence of the substring in the string. If the substring isn't in the string, instr returns 0.
instr("The Voyage of the Beagle", →"Voyage")
returns a value of 5, whereas
instr("The Voyage of the Beagle", "Trip")
returns a value of 0. To test for the occurrence of a substring in a string and get a Boolean result, compare the result of instr with 0 (Figure 4.25):
instr(theString,theSubstring) > 0
Figure 4.25 Using the instr function to search for one string inside another.
Tip
Information stored in computers is normally measured in bytes; kilobytes; megabytes; or if there is a lot of it, gigabytes. A single byte of text is equal to one charactersometimes. Sometimes, it isn't. REALbasic supplies string functions for both situations. Normally, you'll use len. But if you know that the string represents binary data rather than characters, use lenb. Other string functions (such as left, mid, and right) also have their binary versions (leftb, midb, and rightb).
To replace part of a string:
If s1, s2, and s3 are strings,
replace(s1,s2,s3)
returns s1 with the first occurrence of s2 replaced by s3.
If s3 is empty, this same code deletes the first instance of s2 in s1.
If s1 or s2 is empty, the code does nothing.
To replace all instances of s2 in s1 with s3, use
replaceAll(s1,s2,s3). replace and replaceAll are case-
insensitive; they treat uppercase and lowercase letters the same.
To remove spaces from the beginning or end of a string:
Use one of the following:
-
dim s as string s = " Holly Golightly " s = trim(s) leaves "Holly Golightly" in s
(Figure 4.26).
Figure 4.26 Functions for manipulating strings.
-
dim s as string s = " Holly Golightly " s = ltrim(s) leaves "Holly Golightly " in s.
-
dim s as string s = " Holly Golightly " s = rtrim(s) leaves " Holly Golightly" in s.
To change the case of string data:
This string function returns the string given to it, with all uppercase letters converted to lowercase:
lowercase(s1)
This one converts all the characters to uppercase:
uppercase(s1)
And this one converts all the characters to lowercase and then converts the first letter of each word to uppercase:
titlecase(s1)
The result, for example, is This Is Titlecase (Figure 4.27).
Figure 4.27 Functions for controlling the case of string data.
To determine the ASCII value of a string:
Use the asc or ascb function:
i = asc("z") newAsc = asc(c1) + 32
To convert a number to a string:
Use one of the following:
dim s as string, n as integer
n = str(s)
dim s as string, n as integer
n = cstr(s)
The cstr function uses the settings in your International Preferences dialog or Numbers control panel to interpret numbers. Use it if your Mac is not using a period (.) as the delimiter for decimal numbers.
dim s, fmtspec as string, n as integer
s = format(n,fmtspec)
The format function gives you detailed control of the formatting of a number as a string. The fmtspec parameter specifies the formatting according to a simple code. See the Online Language Reference for details (Figure 4.28).
Figure 4.28 Converting a number to a string.
To convert a string to a number:
Use one of the following:
-
dim n as double n = val("27")
puts the value 27 into n.
-
dim n as double n = cdbl("27")
does the same, but uses the settings in your Mac's International Preferences dialog or Numbers control panel to determine how to interpret the number.
Both functions, val and cdbl return a double as their result. The string passed to these functions is assumed to represent a decimal number unless it is prefixed by &O, &H, or &b, which indicate an octal, hexadecimal, or binary number, respectively. The string that follows this prefix must be appropriate to that number's base. A binary number can contain only 1s and 0s, for example. Both functions ignore any characters after and including the first character that is not a proper character in the indicated base (Figure 4.29).
Figure 4.29 Converting a string to a number.
To assign a numeric value to a variable:
Use one of the following:
-
dim n as integer i = 365
An integer is a whole number in the range -2,147,483,648 to 2,147,483,648.
-
dim n as single i = 3.14159
A single is a real numberthat is, a decimal number. It takes up 4 bytes of memory.
-
dim n as double i = 3.14159
A double is a real numberthat is, a decimal number. It takes up 8 bytes of memory, double the number of bytes used by a single. Doubles give higher precision in calculations than singles do, but calculations with doubles run slower.
To do math with variables:
You can combine mathematical values by using any of the basic mathematical operators in any combination (Table 4.1).
Table 4.1 Mathematical Operators
Operator
Meaning
Use
+
Addition
A + B
-
Subtraction
A - B
-
Negation
- B
*
Multiplication
A * B
/
Division
A / B
\
Integer division
A \ B