1. The type Keyword
Before defining a Record or an Enumeration, you must use the type keyword. The type section is used to define new custom data types based on built-in types or by combining existing ones.
The type block is typically placed in the interface section of a unit if the new type needs to be visible to other units.
// Example of a custom type section
type
// 1. Defining a new type name for a standard type
TMemberID = Integer;
// 2. Defining a new array type
TListOfNames = array[1..10] of String;
// 3. Defining a record (complex type)
// ... (Defined below)
2. Records (Structured Data)
A Record is a composite data type that allows you to group different related variables (which can be of different types) under a single name. This creates a single, logical data structure.
- Definition: Defined using the
recordkeyword within thetypeblock. - Access: Members (fields) of a record are accessed using the dot notation (
.).
Example: Defining and Using a Record
// In the interface section
type
TUserInfo = record
// Fields within the record
ID: Integer;
FirstName: String;
IsActive: Boolean;
LastLogin: TDateTime; // TDateTime is a built-in Delphi type for date/time
end;
// In the implementation section (or a procedure)
procedure TForm1.LoadUser;
var
// Declare a variable of the new Record type
CurrentUser: TUserInfo;
begin
// Accessing and assigning values to record fields
CurrentUser.ID := 101;
CurrentUser.FirstName := 'Jane';
CurrentUser.IsActive := True;
// Checking a value
if CurrentUser.IsActive then
ShowMessage('User ' + CurrentUser.FirstName + ' is logged in.');
end;
The with Statement
Accessing record fields can become verbose. The with statement allows you to temporarily set a default object or record, eliminating the need to repeat the record variable name.
// ... continuing the example above
procedure TForm1.LoadUserWith;
var
CurrentUser: TUserInfo;
begin
CurrentUser.ID := 102;
// The 'with CurrentUser do' block allows direct access to fields
with CurrentUser do
begin
FirstName := 'Bob'; // Instead of CurrentUser.FirstName
IsActive := False; // Instead of CurrentUser.IsActive
ShowMessage('User ID: ' + IntToStr(ID));
end;
// Execution leaves the 'with' scope, and direct access is required again
end;
Caution: Use
withsparingly. In complex code, it can make it unclear which record or object a field belongs to if multiple records share the same field name.
3. Enumerations (Custom Lists)
An Enumeration (or Enum) is a special data type that consists of a set of named integer constants. It provides a way to create a list of defined, valid choices, making code much more readable than using raw integer values.
- Definition: Defined by listing the named values separated by commas, enclosed in parentheses.
- Indexing: By default, the first named constant in the list is assigned the integer value 0, the second is 1, and so on.
Example: Defining and Using an Enumeration
// In the interface section
type
TTrafficLightColor = (tlRed, tlYellow, tlGreen);
// In the implementation section (or a procedure)
procedure TForm1.SetTrafficColor;
var
CurrentColor: TTrafficLightColor;
begin
CurrentColor := tlRed; // Assign using the meaningful name
if CurrentColor = tlRed then
begin
ShowMessage('STOP!');
CurrentColor := tlGreen; // Change the state
end;
// You can also use the enumeration value in a case statement:
case CurrentColor of
tlRed: Memo1.Lines.Add('Red light phase.');
tlGreen: Memo1.Lines.Add('Green light phase.');
// No need for a default 'else' if all are covered
end;
end;
Built-in Enumeration Functions
Delphi provides standard functions for working with enumerations, exploiting their underlying integer nature:
| Function | Purpose | Example |
Ord(Enum) | Returns the integer value (ordinal) of the enumeration constant. | Ord(tlRed) returns 0. |
Succ(Enum) | Returns the next constant in the list. | Succ(tlRed) returns tlYellow. |
Pred(Enum) | Returns the previous constant in the list. | Pred(tlYellow) returns tlRed. |
