Defines a set of up to 255 distinct values
1 keyword Set(type Name = Set of Ordinal type;
2 type Name = Set of Value range;
The Set keyword defines an set type for up to 255 discrete values. Do not confuse with enumerations - they can only adopt one value. A set variable always holds all set values - some are set on, some are set off.
The Ordinal type can be :
Characters  such as 'A' and '5'
Integers  in the range 0 to 255
Enumerations  names such as Diamonds, Clubs

Remember that a Set declaration is a variable. As such, it has all possible values set off at the start.
When you initialise the set, you will be setting some or all of these values on.
You can then check a variable to see if its value is in the set. You might want to do this, for example, when parsing code, to see if the next character is a lower case letter.
Each enumeration type, or set member, occupies one bit of a 256 bit (32 byte) mask.

Any of these 32 bytes that contains none of the enumeration definition bits is omitted from the set in order to save storage. See the examples for an illustration of this.
Related commands
ExcludeExclude a value in a set variable
InUsed to test if a value is a member of a set
IncludeInclude a value in a set variable
 Download this web site as a Windows program.

Example code : Sets of characters
  Alphabet  : Set of 'A'..'z';    // Set of all letters

  Alphabet  := ['A'..'z'];        // Set all of the members ON

  // Check values against the alphabet set
  if 'Z' in Alphabet
  then ShowMessage('Z is in Alphabet')
  else ShowMessage('Z is NOT in Alphabet');

  if 'd' in Alphabet
  then ShowMessage('d is in Alphabet')
  else ShowMessage('d is NOT in Alphabet');

  if '1' in Alphabet
  then ShowMessage('1 is in Alphabet')
  else ShowMessage('1 is NOT in Alphabet');
Show full unit code
  Z is in Alphabet
  d is in Alphabet
  1 is NOT in Alphabet
Example code : Sets of positive numbers
  SmallNums : Set of 0..55;        // Set of the first 56 set members
  LargeNums : Set of 200..255;    // Set of the  last 56 set members
  FullNums  : Set of 0..255;      // Set of the  all 256 set members

  // We have a range of 0 to 55 values that we can set
  // Notice the great flexibility we have in setting values
  // We can specify multiple ranges, and individual members
  SmallNums := [3..12,23,30..32];  // Set only some of the members on

  // Show the storage taken by these sets
  // Each set member takes up one bit of one byte of a possible 32 bytes.
  // However, only bytes with at least one bit set are stored
  ShowMessage('SmallNums uses : '+IntToStr(SizeOf(SmallNums))+' bytes');
  ShowMessage('LargeNums uses : '+IntToStr(SizeOf(LargeNums))+' bytes');
  ShowMessage('AllNums uses   : '+IntToStr(SizeOf(FullNums))+' bytes');

  // Check values against the small numbers set
  if 12 in SmallNums
  then ShowMessage('12 is in SmallNums')
  else ShowMessage('12 is NOT in SmallNums');

  if 13 in SmallNums
  then ShowMessage('13 is in SmallNums')
  else ShowMessage('13 is NOT in SmallNums');

  if 30 in SmallNums
  then ShowMessage('30 is in SmallNums')
  else ShowMessage('30 is NOT in SmallNums');
Show full unit code
  SmallNums uses : 7 bytes
  LargeNums uses : 7 bytes
  AllNums uses   : 32 bytes
  12 is in SmallNums
  13 is NOT in SmallNums
  30 is in SmallNums
Example code : Sets of user defined enumeration values
  TNumber   = (Ace, One, Two, Three, Four, Five, Siz, Seven, Eight,
  Nine, Ten, Jack, Queen, King);

  CourtCards: Set of TNumber;      // Court cards
  CardNumbers : array[1..4] of TNumber;
  i : Integer;

  // Set up the card numbers only to allow picture cards
  CourtCards := [Ace, Jack, Queen, King];

  // Define some cards
  CardNumbers[1] := Ace;
  CardNumbers[2] := Four;
  CardNumbers[3] := Jack;
  CardNumbers[4] := Seven;

  // Show the picture cards that we have
  for i := 1 to 4 do
    if CardNumbers[i] in CourtCards
    then ShowMessage('Card '+IntToStr(i)+' is a court card');
Show full unit code
  Card 1 is a court card
  Card 3 is a court card
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page