Chapter 7. Variables
Variables are placeholders that are used in a program in order to provide a method of accessing the objects that are acted upon by the program. They are really like the glue that holds everything together. In this chapter we will discuss how variables are used in a SIMPOL program. We will discuss the various types, how to create them, their visibility within the program, how some variables can hold more than one type of object, and how variables affect the objects they represent.
Variable Typing
SIMPOL is what is known as a strongly typed language. By
this is generally meant that it is considered an error to assign an object or value of
one type to a variable of another type. As an example, if I have one variable that is
declared to be of type integer
it is an error to assign a variable of
type number
to that variable. It will result in a type mismatch error.
This is one way that the programming language protects the programmer from making
an error that might otherwise be very hard to find. If instead of generating an error,
the programming language automatically converted the variable of type
number
to an integer value, truncating or rounding the non-integer
portion of the value, it would be very difficult to track down, especially in a large
program since the problem may only appear to be intermittent (it would only occur
when the value in the variable of type number
was not an integer
value).
That is all well and good, but there are some situations where it is absolutely essential
to be able to handle more than one type using only one variable. As an example,
consider the situation where you may wish to process all of the controls on a form.
Each form control has its own type. If it is possible to declare a variable to be of
type FormControl
and if that type is designed to represent
any form control, then it would then be possible to use a single
variable to contain a reference to any control on the form, without causing an error. In
SIMPOL by using the type
property of the object it would then be
possible to detect which type the variable currently contains and to perform appropriate
operations on that object. Later in this chapter this capability, also known as polymorphism,
is discussed in greater detail.