Pascal Programming/Sets

< Pascal Programming

Sets

With enumerations, there comes the opportunity to use sets. Sets store multiple values of an enumeration. They act as a set of switches, so to speak. I suppose an example should give a clearer meaning to this.

type
  Skills = ( Cooking, Cleaning, Driving, Videogames, Eating );

var
  Slob: set of Skills = [Videogames, Eating];

Here, we've made a variable Slob, which represents a set of the Skills enumeration. By default, set types are usually stored internally as LongInts if they have under 32 values. Anything above that, and they are stored as one byte per element. Modern compilers limit set types to up to 255 values maximum.

Putting set variables to use in code is as follows:

Slob := Slob + [Driving]; // Append a value to our existing Slob variable

if Videogames in Slob then // if Slob has the Videogames value...
  writeln('Is he a level 45 dungeon master?');
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.