by Dr. Peter Wayne
| Understanding the scope of a variable is key to successful Xbasic programming. |
A variable is nothing more than a location in your computer's memory in which you want to store a value. Alpha Five allows many different types of variables. The most common variable types are character, numeric, date, and logical. There are also pointer variables and blob variables.
You can assign a value to a variable with the assignment operator, "=". For example, this line assigns "Alpha Five" to the character variable, "prog":
prog="Alpha Five"
You can explicitly tell Alpha Five what type of variable you are using by declaring the variable in a dim or dimension statement. For example, we can type this in the Interactive code editor window:
dim x as c x="Alpha Five"
Once a variable has been declared as one type, it cannot be reassigned to another type. Look what happens if we try to assign a number to a character variable:
dim x as c x="Alpha Five" x=2 ERROR: variable type mismatch
You can access the value of a variable with the typeof() function:
dim x as c x="Alpha Five" x=2 ERROR: variable type mismatch ? typeof(x) = "C" dim dte as d dte=date() ? typeof(dte) = "D"
Now, all this has been just a warm-up for our main subject, which is variable scoping. The scope of a variable determines how long the variable persists in Alpha Five's memory and which other scripts and functions can access the variable.
Variable scope can be either
In Alpha Four, all programmer-defined variables persist for the life of your interactive session, and are available to any keystroke script. In programming jargon, Alpha Four's variables are all global variables.
Variables in Alpha Five scripts are local unless explicitly declared otherwise. You declare the type of a variable in its "dim" statement. Session-level variables are declared with the shared keyword, and global variables with the global keyword. For example, in this script "x" in the function is different from "x" in the rest of the script:
' local variables script
function local_var as n(input as n)
dim x as n
x=2*input
ui_msg_box("in the local_var function, local variable x is",str(x))
local_var=x
end function
'
dim x as n
x=10
ui_msg_box("in the larger script, x is",str(x))
y=local_var(x)
ui_msg_box("returning to the larger script, x is still",str(x))
If you type this script in and run it, you will see that "x" in the main script starts off as 10, is 20 in the function body, and then is 10 in the main function body again. If we revise the script to use session-level variables, then "x" will be the same in the function and in the main script:
''XBasic
' session variables script
function session_var as n(input as n)
dim shared x as n
x=2*input
ui_msg_box("in the session_var function, session variable x is",str(x))
session_var=x
end function
'
dim shared x as n
x=10
ui_msg_box("in the larger script, x is",str(x))
y=local_var(x)
ui_msg_box("returning to the larger script, x is now",str(x))
Notice that it is not enough to simply declare "x" as a session-level or shared variable in the main body of the script. If I want the function to be able to access the same variable, then I need to include dim shared x as n in the function as well. The same holds for global variables. If you want to share a variable among a form, a script, and a report, then you have to make it a global variable in the form design, report design, and in the script's variable declaration.
One last script. Do you understand what is happening in this script?
''XBasic
' change of variable types script
function local_var as n(y as n)
dim x as c
x=str(y)
ui_msg_box("in local_var, local variable x is of type",typeof(x))
ui_msg_box("in local_var, local variable y is of type",typeof(y))
end function
'
dim x as n
dim y as c
x=10
ui_msg_box("in the larger script, x is of type",typeof(x))
ui_msg_box("in the larger script, y is of type",typeof(y))
y=local_var(x)
I don't recommend you write code like that in this script - it can be very confusing! It's a good idea to adhere to some conventions and rules for variable use:
Tom Cone has put together a form to help you in understanding variable scope. You can download this form and experiment with it.
8/23/99
Don't forget, we need your feedback to make this site better!