| M206 Smalltalk
tutorial . Variables:- Temporary, Local, Instance, Class, Global,
Tutorial 5. Variables
| 1. Starting Smalltalk, and Terminology | 2. Precedence Rules | 3. Classes - Strings | 4. Class Browser | 5. Variables | 6. Error Messages | 12. Control Structures | 16. Dialogs | 23. Collection class | 39. OpenGUI |
| Smalltalk Books | Student Software & Books | Hints & Tips | Sites | Download Smalltalk | Coloured code | Download Documents | ASCII Codes |
|Updated 15 Oct 2001|
There are 6 kind of variables in the M206 Open University version of Smalltalk, less in other versions.
The scope is listed under each type of variable below.
ScopeLocal variables can only be used in the LearningBook section in which they are created.
ScopeInstance variables can only be assessed directly from the methods of their class and subclasses of that class. Normally this is only done by the instance variable's get and set method.
ScopeCan be used directly by the methods of the class, its subclasses, instances of the class or any of its subclasses.
Global variables are used to give easy access to commonly used objects. Such as
Temporary variableExamine the following 3 line expression series. Do not evaluate until after you attempt to answer questions 4a-c.
| myName | myName := 'John'. Dialog warn: 'Hello', myName
| myName |
|Declares a temporary variable called myName.
A decimal point is NOT required after a temporary variable
After this line and before the next line the value of myName is nil
myName := 'John'.
|The assignment message := assigns
the string literal 'John' to the variable myName.
Decimal point required.
|Displays a dialog box on the screen. See Tutorial 16. Dialog warn: Note: A Dialog warn: box answers with nil|
|Code for the wording that appears in the dialog box. Note the concatenation of string literal 'Hello' and the value of temporary variable myName by using the comma ,|
Dialog warn: 'Hello', myName
|Final line in expression therefore decimal point is optional|
a. Write down the exact message that you expect will be displayed in the Dialog box
b. After clicking the button in the Dialog Box, what message answer should appear in the Workspace Display Pane?
c. Will the temporary variable myName appear in the Workspace Variables list pane.
Go to Answers 1a-c.
Exercise 1. Evaluate ALL of the following 3 line expression series, below in 1 go.
I.e. Highlight all 3 lines and click evaluate.
| myName | myName := 'John'. Dialog warn: 'Hello', myNameCheck that the answers to questions 1a-c are correct.
Question 1d. What button appeared on the dialog warn box?
Note: Dialog warn boxes always have this single button.
Go to answer 1d.
Question 1e. It is required that a space is inserted between Hello and John in the dialog box.
Suggest 3 ways of altering the following to achieve this space!
| myName | myName := 'John'. Dialog warn: 'Hello', myNameGo to answer 1e.
What results do you expect if you just evaluate the single line:-
| myName |Go to answer 2.
Evaluate and check result is as per answer 2
You have just declared myName as a temporary variable in exercise 2. State what you think will be the result of evaluating the single line
myName := 'John'.Go to answer 3.
Carry on straight away with Exercise 3
Local VariablesExercise 3. Evaluate and check result is as per answer 3. Clicking the Create it button will declare myName as a local variables. This will place myName in the variable list pane.
Check that myName is in the Variable list pane. If this is not the case pay particular attention to answer 3, and carry out exercise 3 again.
myName is now a local variable, its scope is different from its previous use as a temporary variable. To demonstrate the difference in the scope you will carry out exercises similar to those in the previous temporary variable section.
Exercise 4. Evaluate
| myName |
You will be informed that myName is already defined (perhaps in an outerscope).
Click the cancel button.
This is informing you that the variable is already declared. Thus proving that the scope is much wider than that of the temporary variable. In fact you can close this LearningBook, save it, then re-open the same book, the local variable and its value still exists. The value is still nil until the next exercise.
myName := 'John'. Dialog warn: 'Hello', myNameWorks exactly as expected, setting the value of myName to 'John', and displaying the dialog box, and the Dialog warn: box answering with nil.
State 3 ways you can find out the current value of the variable myName.
Go to answer 4.
You are now required to place a space so that the Dialog box displays Hello John instead of HelloJohn.
If you have not tried the ways in answers 1-3, you may like to do that now. These are fairly straight forward, and if you fully understand the alterations made it is not necessary to carry them out.
In the before you try the final alternative way.
Because this is a tutorial on variables we will use an additional variable that will be named aSpace to provide a solution.
First you will remove the local variable myName.
| myName | myName := 'John'. Dialog warn: 'Hello', myName
Go to Solution 2
Remove the declaration line, allow the 2 local variables to be created.
Instances of a class.Local variables that reference instances of a class are created using expressions, as follows, and clicking on the create it button
Class VariablesThere are very few examples of using class variables in the M206 OU course. You are likely going to be using class methods and possibly class variables in a TMA. This question appears to create problems for students.
As you do the course note how you use class methods.
The message warn: has been sent to the Dialog class.
In LB 22 you will create a class variable FrogCount.
Also in LB 22 you will work with the Date class. The date class has 5 class variables.
Answer 1a. The message will be:
Note there are no single or double quotes, and also no space between Hello and John. You will shortly be asked to rectify this and insert a space.
Answer 1b. Nil will appear in the Workspace Display Pane
Answer 1c. Temporary variables do NOT appear in the Workspace Variables list pane.
Return to question 1a-c.
The box had an OK button.
Return to question 1d.
You could use any of the following alternatives. If you like you can try them.
Return to question 1e..
Smalltalk realises that you have just declared a temporary variable, and that you have not used that temporary variable and warns you with a pop up window that states myName is never used with buttons Remove it, proceed, Cancel.
Click the proceed button which ensures that the line is evaluated and the temporary variable is declared.
When developing complicated methods you may wish to build the method up slowly, checking how the unfinished method is working at its current step in it's development. You could therefore get this message, its is OK to proceed if this is required.
Return to question 2.
Smalltalk has already forgotten that you declared the temporary variable, because the scope for the temporary variable was in the expression series i.e. the single line | myName| in which it was declared.
In exercise 2. It does not recognise myName and therefore assumes it is an undeclared variable and a window pops up stating myName is undeclared with buttons Create it, correct it, Cancel.
Click on the Create it button.
Return to question 3.
Return to question 4.
| myName aSpace | aSpace := ' '. myName := 'John'. Dialog warn: 'Hello', aSpace, myNameNotes:
Copyright © John McGuinn 2000 -01