»  The Delphi Column 

  SET

In this column, we have a detailed look at SETS in Delphi.

A set allows you to have a combination of values of the same ordinal type (ordinal types include integer, character, Boolean, enumerated, and subrange types).
A set can contain up to 255 values.
Note: you can not have a "set of string" or a something similar, because "string" is not an ordinal type.

You will see sets in the properties of a lot of Delphi components, such as BorderIcons of a form, the Style of a font,
for example: Font.Style := [fsBold, fsItalic] , and so on.

Notation

In sets, you will often encounter "subranges".

A subrange is a subset of the values of some ordinal type.
A subrange is written in the form LOW..HIGH, where LOW and HIGH are constant expressions.
The subrange includes all the values from LOW to HIGH.
For example, the subrange 1..5 includes the integer values 1, 2, 3, 4 and 5.
Another example: the subrange 'a'..'c' includes the character values 'a', 'b' and 'c'.
 

Set variables

A set variable contains a collection of up to 255 values of the same ordinal type.
You can declare a set variable in two ways:

  • either with the syntaxis "Varname: Vartype", such as:

    var Digits: TDigits; Nums: TNums;

  • or with the syntaxis "Varname: set of ...", such as:

    var Digits: set of '0'..'9'; Nums: set of 1..100; B: set of Byte;

If you use the first syntaxis, the type that you specify in the variable declaration must have been defined already. For example:

type TDigits = set of '0'..'9'; var Digits: TDigits;

Set types

A set type can be used in the declaration of a set variable. Examples:

type TDigits = set of '0'..'9'; // numeric digit characters TNums = set of 1..100; // integer numbers

Set constants

A set constant can contain up to 255 discrete values. Example:

const Abc = ['a', 'b', 'c'];

Note: you can also use a "constant expression" that is constructed "on the fly", such as in:

if Ch in ['a', 'b', 'c'] then ...

Including and excluding values

Nums := [10..100]; // include range of values Nums := [1, 10..100]; // include individual values and a range Include(Nums, 9);     // include an individual value Exclude(Nums, 99);    // exclude an individual value

Nums now contains the following set of values: [1, 9..98, 100]

Contitional testing

"IN" test if a set contains a value: if 20 in IntNums then ...

= test if sets are identical: if IntNums1 = IntNums2 then ...

<> test for non-identical sets: if IntNums1 <> IntNums2 then ...

>= test if a set is a superset of another: if IntNums2 >= IntNums1 then ...

<= test if a set is a subset of another: if IntNums2 <= IntNums1 then ...

Set operators

+  returns the union of sets
*  returns the intersection of sets
-  returns the difference of sets

Example code

procedure TForm1.Button1Click(Sender: TObject);
var
  N1, N2, N3, N4, N5: set of 1..9;
  i: integer;
begin
  N1 := [1, 2, 3];
  N2 := [1, 2, 4];
  N3 := N1 + N2;   // N3 contains [1, 2, 3, 4]
  N4 := N1 * N2;   // N4 contains [1, 2]   
  N5 := N1 - N2;   // N5 contains [3]
  for i := 1 to 9 do
    if i in N5 then 
      ShowMessage(IntToStr(i) + ' is in set N5');
end;


Database Tutorials  FAQ  Crash Course Delphi  Tips  Source Code  Downloads  Links

© Copyright 1999-2019 
DelphiLand