Explicit Declaration of Variables
Up to now, all the variables you have worked with have been declared implicitly. You created variables in the Interactive Window by simply specifying a valid variable name and then assigning it a data value. The type of the data you assigned to the variable determined its type.
There is an Xbasic command that can be used to define a variable and to specify its type. The command is DIM. When you use DIM you define a variables name and the type of data it can hold.
| DIM comes from the word dimension. When you define a variable, it is referred to as dimming or dimensioning the variable. When you DIM a variable you are setting its dimensions. |
For example, the following lines of Xbasic define four different variables. Defining the variables with the DIM command tells Alpha Five to setup storage locations with the names and to expect those variables to store data of the type specified.
DIM first_Name as C DIM age as N DIM date_of_birth as D DIM married as L |
Once a variable has been defined with the DIM command, it is available for use in subsequent Xbasic commands. So after issuing the commands above, you could then have two commands like this:
date_of_birth = {11/21/1974} age = int((date() - date_of_birth)/365.25) |
| How the Expressions are Evaluated int((date() - date_of_birth)/365.25) Alpha Five evaluates (calculates) this expression as follows. It uses these two rules.
First Alpha Five evaluates the sub-expression: (date() - date_of_birth) Alpha Five substitutes the appropriate date (the current date is returned by the date() function and whatever data is stored in the date_of_birth variable. This is the number of days that have passed since the birth date. Lets say date() returns {06/15/2002} and the date_of_birth variable is equal to {11/21/1974}. Then the sub-expression equals ({06/15/2002} - {11/21/1974}) or 10068 Now the expression is int(10068/365.25) Applying the parentheses rule, Alpha Five calculates 100068 divided by 365.25, which equals 27.564682 Finally, the expression is now: int(27.564682) The int() Xbasic function takes a numeric value and returns the integer portion (dropping any decimal value). So the final result of the expression is: 27 This value is assigned to the age variable. |
You do not have to use DIM to define variables as we have already seen. In fact, you can also use DIM to name a new variable without specifying its data type. The data type is determined only after you assign data to that variable. So you could type the following two commands:
DIM age age = "Eighteen" |
The DIM command tells Alpha Five to set aside storage for a variable named age. But only after the second command is processed does Alpha Five know to treat age as a character data type variable.
Once a variable has been assigned a value or dimmed, it can only store data of that type. If you want to change the type of data that a variable can hold, you can use the REDIM command to redefine it. (Note: If any data is stored in the variable before you REDIM it, that data will be discarded).
Exercise 5 - Dimensioning and using variables
Using DIM to define a variable is also known as Declaring a variable. Declaring variables with DIM at the beginning of a code file is good programming practice. It makes your variables and their types explicitly known and readily apparent for someone reading your code later.
Declare the following variable using the DIM command.
DIM birthday as D |
Use the typeof() function to check the data type of the variable birthday.
? typeof(birthday) |
Alpha Five displays D indicating a type of date.
Assign your birthday to the variable birthday (enter your own birthday or use this date).
birthday = {11/21/1974} |
Now type:
? Int((date() - birthday)/365.25) |
Alpha Five displays your age.
Now type the following:
birthday = "Twenty-seven" |
Inside a script, Alpha Five would display an error. In this environment, it sets the value of birthday to NULL.
? birthday = { / / } |
| Type conversion Sometimes you may want to get data of one type out of a variable and store it in a variable that holds data of a different type. For example, suppose you have a variable defined as a character variable storing a products weight. product_Weight = 42 ' lbs You want to multiply the weight in pounds by $ .75 per pound to calculate a shipping price. You can use a type conversion function to get the numeric information from the product_Weight variable and store it in a numeric variable. The function that converts from a character string value to a numeric value is called VAL(). When VAL() is used on a character value, it looks for any numbers occurring at the beginning of the string, drops remaining characters, and then converts the number string (the number stored as Characters) to a numeric value. So VAL(product_Weight) returns the numeric value 42. If you had a numeric variable named ship_Cost you could write this expression: ship_Cost = VAL(product_Weight) * .75 to store a numeric shipping cost in the ship_Cost variable. |
Next