Sets


Sets are collections of ordinal values that can have a maximum of 256 elements. To declare a set, you need to create an enumerated type first. Then use the following syntax to declare a set:

TNewSet = set of EnumeratedType;

Sets are really lightweight and give us the ability to work with a collection of values. A set variable occupies a single byte of memory. The following code shows how to declare a new set based on the TDay enumeration and how to assign an empty set to the ThisWeek set variable:

type   { enumerated type }   TDay = (Monday, Tuesday, Wednesday,     Thursday, Friday, Saturday, Sunday);   { set }   TDays = set of TDay; var   ThisWeek: TDays; begin   ThisWeek := []; // the set contains no values end.

Working with Sets

The assignment operator allows you to assign one or several values to a set in one statement. To assign values to a set, you have to enclose them in brackets. If you're assigning multiple values to the set, the values inside the brackets must be separated with commas:

ThisWeek := [Monday];  ThisWeek := [Monday, Tuesday, Friday];

The + and – operators can be used to add or remove values from a set:

ThisWeek := ThisWeek - [Friday];  ThisWeek := ThisWeek + [Saturday, Sunday];

These operators are usually used to add or remove elements from the set. However, if possible, you should use the Exclude and Include procedures instead because the procedures work faster than the operators:

Exclude(ThisWeek, Friday);  Include(ThisWeek, Saturday);  Include(ThisWeek, Sunday);

The difference between the operators and the Exclude and Include procedures is that operators can work with multiple values ([Saturday, Sunday]), while procedures only accept one value at a time.

Set Membership

The reserved word in can be used to determine if a specified value exists in the set. For instance, to find out if the ThisWeek set contains Wednesday, you can write this:

if Wednesday in ThisWeek then   WriteLn('Have to work on Wednesday.');

You can browse through a set using any of the available loops, even the for-in loop, which also requires the least amount of typing and results in the most readable code.

Listing 7-1: Working with sets

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; type   TDay = (Monday, Tuesday, Wednesday,     Thursday, Friday, Saturday, Sunday);   TDays = set of TDay; var   ThisWeek: TDays;   Day: TDay; const   DAY_TEXT: array[TDay] of string = ('Monday', 'Tuesday',     'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'); begin   ThisWeek := [Monday, Tuesday, Friday];   for Day in ThisWeek do     WriteLn('I have to work on ', DAY_TEXT[Day], '.');   { If you need to use a compiler older than Delphi 2005. }   for Day := Monday to Sunday do     if Day in ThisWeek then       WriteLn('I have to work on ', DAY_TEXT[Day], '.');   ReadLn; end.
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net