Difference between revisions of "Set"
m (→Manipulating sets: internal link) |
(rewoed, internal links) |
||
Line 5: | Line 5: | ||
A Set encodes many values from an enumeration into an Ordinal type. | A Set encodes many values from an enumeration into an Ordinal type. | ||
− | For example let's consider this | + | For example let's consider this enumeration: |
<syntaxhighlight> | <syntaxhighlight> | ||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Constant instances of TPossibleSpeeds can be defined using brackets to hold set elements: | |
<syntaxhighlight> | <syntaxhighlight> | ||
Line 29: | Line 29: | ||
== Manipulating sets == | == Manipulating sets == | ||
− | + | Two functions defined in the [[RTL]] [[System unit]] are used to manipulate a set: [[Include]](ASet,AValue) and [[Exclude]](ASet,AValue). | |
<syntaxhighlight> | <syntaxhighlight> |
Revision as of 08:34, 20 August 2016
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
Introduction
A Set encodes many values from an enumeration into an Ordinal type.
For example let's consider this enumeration:
TSpeed = (spVerySlow,spSlow,spAVerage,spFast,spVeryFast);
And this set:
TPossibleSpeeds = set of TSpeed
Constant instances of TPossibleSpeeds can be defined using brackets to hold set elements:
const
RatherSlow = [spVerySlow,spSlow];
RatherFast = [spFast,spVeryFast];
RatherSlow and RatherFast are some Set of TSpeed.
Manipulating sets
Two functions defined in the RTL System unit are used to manipulate a set: Include(ASet,AValue) and Exclude(ASet,AValue).
var
SomeSpeeds = TPossibleSpeeds;
begin
SomeSpeeds := [];
Include(SomeSpeeds,spVerySlow);
Include(SomeSpeeds,spVeryFast);
end;
Sets cannot be directly manipulated if they are published. You usually have to make a local copy, change the local copy and then to call the setter.
procedure TSomething.DoSomething(Sender: TFarObject);
var
LocalCopy = TPossibleSpeeds;
begin
LocalCopy := Sender.PossibleSpeeds; // getter to local
Include(LocalCopy,spVerySlow);
Sender.PossibleSpeeds := LocalCopy; // local to setter.
end;
The Keyword In is also used to test if a value is in a set. It's usually used in this fashion:
var
CanBeSlow: Boolean;
const
SomeSpeeds = [Low(TSpeed)..High(TSpeed)];
begin
CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds);
end;
simple data types |
|
---|---|
complex data types |