Smalltalk Programming/Variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Smalltalk uses variables in the usual Object Oriented way, you move around pointers to data more often than the actual data. A variable is a pointer to an object, through which you could ask that object to report its data, change its data, or perform an action. Care must be taken when altering an object, it may affect other variables pointing to that object elsewhere.

There are different degrees of variable at the system, object, and method levels.

The SystemDictionary Smalltalk holds onto the globally accessible variables. These can be easily called upon anywhere, common singleton objects like transcript are named here.

In their class definition, objects have a given set of variables defined. Any object instance can look at its own variables within a method (subclass methods too). If a variable needs to be altered externally, the object may have methods to get the variable in and out.

Smalltalk variable names are declared in the beginning of a method or block separated by the pipe "|" symbol. Example: | t1 t2 t3 | declares three variables named t1, t2, and t3.

To assign a value to a variable, use this format: variableName := value or this format: variableName _ value.

Arrays also can be variables. Arrays can hold any type of value. An array if defined like this: #('Put array items here'). Example: #('string' $C 6) Makes an array with three values that contain a the string "string", the character 'C', and the number 6.