The Delphi Language, Part 1 - Records and Sets
(Page 8 of 10 )
A user-defined structure is referred to as a record in the Delphi language, and it's the equivalent of C#'s struct or Visual Basic .NET's Type. As an example, here's a record definition in Delphi as well as equivalent definitions in C# and Visual Basic .NET:
{ Delphi }
Type
MyRec = record
i: Integer;
d: Double;
end;
/* C# */
public struct MyRec
{
int i;
double d;
}
'Visual Basic
Type MyRec
i As Integer
d As Double
End Type
When working with a record, you use the dot symbol to access its fields. Here's an example:
var
N: MyRec;
begin
N.i := 23;
N.d := 3.4;
end;
Methods, operator overloads, and interfaced implementations are also supported for records. This capability was not supported on previous versions of the Delphi compiler. These topics are covered in more detail, along with class types, later in this chapter. However, the following code example shows the syntax for using these elements with records:
IBlah = interface
procedure bar;
end;
Foo = record(IBlah) // record implements IBlah interface
AField: Integer;
procedure bar;
class operator Add(a, b: Foo): Foo; // overload + operator
end;
Sets
Sets are a uniquely Delphi type that have no equivalent in C# or Visual Basic .NET. Sets provide an efficient and convenient means of representing a collection of ordinal, AnsiChar, or enumerated values. You can declare a new set type using the keywords set of followed by an ordinal type or subrange of possible set values. Here's an example:
type
TCharSet = set of AnsiChar; // possible members: #0 - #255
TEnum = (Monday, Tuesday, Wednesday, Thursday, Friday);
TEnumSet = set of TEnum; // can contain any combination of TEnum members
TSubrangeSet = set of 1..10; // possible members: 1 - 10 Note that a set can only contain up to 256 elements. Additionally, only ordinal types can follow the set of keywords. Therefore, the following declarations are illegal:
type
TIntSet = set of Integer; // Invalid: too many elements
TStrSet = set of string; // Invalid: not an ordinal type Sets store their elements internally as individual bits, which makes them very efficient in terms of speed and memory usage.
Note - If you are porting Win32 code to .NET, bear in mind that chars are 2-bytes in the .NET world and 1-byte in Win32. This means that a set of Char declaration in .NET is demoted by the compiler to a set of AnsiChar, which can potentially change the meaning of code. The compiler will issue a warning to this effect, recommending that you explicitly use set of AnsiChar.
Using Sets Use square brackets when constructing a literal set value from one or more elements. The following code demonstrates how to declare set type variables and assign them values:
type
TCharSet = set of AnsiChar; // possible members: #0 - #255
TEnum = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
TEnumSet = set of TEnum; // can contain any combination of TEnum members
var
CharSet: TCharSet;
EnumSet: TEnumSet;
SubrangeSet: set of 1..10; // possible members: 1 - 10
AlphaSet: set of 'A'..'z'; // possible members: 'A' - 'z'
begin
CharSet := ['A'..'J', 'a', 'm'];
EnumSet := [Saturday, Sunday];
SubrangeSet := [1, 2, 4..6];
AlphaSet := []; // Empty; no elements
end;
Set Operators
The Delphi language provides several operators for use in manipulating sets. You can use these operators to determine set membership, union, difference, and intersection.
Membership Use the in operator to determine whether a given element is contained in a particular set. For example, the following code would be used to determine whether the CharSet set mentioned earlier contains the letter 'S':
if 'S' in CharSet then
// do something; The following code determines whether EnumSet lacks the member Monday:
if not (Monday in EnumSet) then
// do something;
Union and Difference
Use the + and - operators or the Include() and Exclude() procedures to add and remove elements to and from a set variable:
Include(CharSet, 'a'); // add 'a' to set
CharSet := CharSet + ['b']; // add 'b' to set
Exclude(CharSet, 'x'); // remove 'z' from set
CharSet := CharSet - ['y', 'z']; // remove 'y' and 'z' from set
Tip - When possible, use Include() and Exclude() to add and remove a single element to and from a set rather than the + and – operators, as the former is more efficient.
Intersection Use the * operator to calculate the intersection of two sets. The result of the expression Set1 * Set2 is a set containing all the members that Set1 and Set2 have in common. For example, the following code could be used as an efficient means for determining whether a given set contains multiple elements:
if ['a', 'b', 'c'] * CharSet = ['a', 'b', 'c'] then
// do something
Unsafe Code
Right about now, those with previous experience in Delphi for Win32 might be thinking, "this all sounds fine so far, but what happened to the pointers?" Although pointers are in the language, they are considered unsafe from a .NET standpoint because they allow for direct access to memory. Therefore, in order to employ pointers in your applications, you will need to let the compiler know that it should permit unsafe code. In order to write unsafe code, you must
Include the {$UNSAFECODE ON} directive in the unit containing the unsafe code.
Mark functions containing unsafe code with the unsafe keyword.
The following unsafe code will successfully compile in Delphi:
{$UNSAFECODE ON}
procedure RunningWithScissors; unsafe;
var
A: array[0..31] of Char;
P: PChar; // PChar is an unsafe type
begin
A := 'safety first'; // fill character array
P := @A[0]; // point to first element
P[0] := 'S'; // change first element
MessageBox.Show(A); // show changed array
end;
Note the use of Delphi's @, or address of operator, to obtain the address of a bit of data.
Tip - Unsafe code is totally discouraged in .NET because an unsafe application will not pass muster with .NET's PEVerify tool and may therefore be subject to greater security restrictions. However, unsafe code can be an excellent way to bridge the gap between the Win32 world and the .NET world during a migration period. It might be difficult to port an entire large application from Win32 to .NET in one fell swoop; however, unsafe code allows you to port one portion at a time, keeping the old code as unsafe until it can be moved in its entirety to safe code.
You can assume that the code in this portion of the chapter must be compiled using the $UNSAFECODE and unsafe directives.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Pointers >>
More .NET Articles
More By Xavier Pacheco