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).
In sets, you will often encounter "subranges".
A subrange is a subset of the values of some ordinal type.
A set variable contains a collection of up to 255 values of the same ordinal type.
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;
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
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]
+ returns the union of sets
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  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