DelphiBasics
Type
Keyword
Defines a new category of variable or process
 keyword Type(?1. type Name = Existing type;
 ?2. type Name = type Existing type;
 ?3. type Name = (EnumValue1 [=value], EnumValue2 [=value] ...);
 ?4. type Name = Expression1..Expression2;
 ?5. type Name = ^Existing type;
 ?6. type Name = array[...] of Existing type;
 ?7. type Name = class ... end;
 ?8. type Name = class of Existing class
 ?9. type Name = dispinterface ... end;
 10. type Name = file of Existing type;
 11. type Name = function ...;
 12. type Name = interface ... end;
 13. type Name = object ... end;
 14. type Name = procedure ...;
 15. type Name = record ... end;
 16. type Name = set of Ordinal values
Description
The Type keyword is a fundamental part of Delphi. Unlike other languages, it allows new categories (types) of variable and process to be created. These newly named categories can then be referenced just as if they were a part of the language.
 
For example :
 
type TCourtCards = (Ace, Jack, Queen, King);
 
allows a new variable of this 'type' to be defined :
 
var Card : TCourtCard; Card := Queen;
 
It is a useful convention to prefix type Names with a T.
 
1.type Name = Existing type
 
Refers to an existing type, such as string by a new Name.
 
2.type Name = type Existing type
 
This has the same effect as above, but ensures that at run time, variables of this type are identified by their new type name, rather than the existing type name.
 
3.type Name = (EnumValue1, EnumValue2 ...)
 
Defines an enumeration type, with values EnumValu1, EnumValue2 and so on. These are user defined names representing all possible values for the enumeration.
 
These values must be unique in your program. Once defined in a type, they can be referenced in two ways : when assigning/referencing a variable of that type, and as a numerical value using the Ord keyword. See right for examples.
 
Note that these enumerations are set to have values 0, 1, 2 etc by position in the definition unless this value is overriden by the =value number.
 
For example :
 
type Days = (Monday = 1, Tuesday, Wed ...);
 
where Monday would be set to 1, Tuesday 2, Wednesday 3 and so on.
 
4.type Name = Expression1..Expression2 Here we have a complete range of integer numbers or characters from Expression1 to Expression2.
 
Expression1 and 2 maybe expressions that evaluate to an integer number or a character, or just integer or character constants.
 
For example:
 
type TAlphabet = 'A'..'z';
 
is often used to define the range of letters from upper case A right through to lower case z.
 
5.type Name = ^Existing type
 
The '^' is a pointer to the existing type. It is often used to navigate through sets of records.
 
6.type Name = array[...] of existing type
 
A structured type, encapsulating an array of types as a new type.
 
7.type Name = class ... end Mechanism for defining a new class. See the Class keyword for full details.
 
8.type Name = class of existing class
 
This provides a meta-class definition. See the Class keyword for details.
 
9.type Name = dispinterface ... end A dispatch interface type. See the Dispinterface keyword for full details.
 
10.type Name = file of Existing type
 
Defines a type to refer to file that contains records of the given existing type (by default, files are treated as containing binary data).
 
11.type Name = function ...
 
Defines a function as a type, allowing the function to be defined as a parameter to a subroutine.
 
12.type Name = interface ... end
 
Mechanism for defining an interface. See the Interface keyword for full details.
 
13.type Name = object ... end
 
Obsolete equivalent to the Class definition.
 
14.type Name = procedure ...
 
Defines a function as a type, allowing the function to be defined as a parameter to a subroutine.
 
15.type Name = record ... end
 
Encapsulates a data structure under the given Name. See the Record keyword for full details.
 
16.type Name = set of Ordinal values
 
The set of Ordinal values defines a range of integer numbers or characters. See the Set keyword for full details of sets.
Related commands
ArrayA data type holding indexable collections of data
ClassStarts the declaration of a type of object class
ConstStarts the definition of fixed data values
FileDefines a typed or untyped file
FunctionDefines a subroutine that returns a value
InterfaceUsed for Unit external definitions, and as a Class skeleton
ObjectAllows a subroutine data type to refer to an object method
ProcedureDefines a subroutine that does not return a value
RecordA structured data type - holding fields of data
SetDefines a set of up to 255 distinct values
VarStarts the definition of a section of data variables
 Download this web site as a Windows program.




 
Example code : Examples of some of these types
Type
  TString1 = string;              //  1. type Name  = Existing type
  TString2 = type string;        //  2. type Name  = type Existing type
  TTemp    = (Hot, Warm, Cold);  //  3. type Name  = (Enum1, Enum2 ...)
  TExpr    = 5*2 .. 6*3;          //  4. type Name  = Expr1 .. Expr2
                                  //  5. See the Pointer keyword
  TArray   = array[1..3] of byte;//  6. type Name  = array[...] of type
                                  //  7. See the expanded code
                                  //  8. See the Class keyword
                                  //  9. See the Dispinterface keyword
                                  // 10. See the File keyword
                                  // 11. See the Function keyword
                                  // 12. See the Interface keyword
                                  // 13. Obsolete
                                  // 14. See the Procedure keyword
  TRecord  = record              // 15. type Name  = record .. end;
               header : string;
               value  : Integer;
             end;
  TLetters = set of 'A'..'z';    // 16. type Name  = set of Ordinals

var
  // Declare variables using the above types
  firstName   : TString1;
  lastName    : TString2;
  temperature : TTemp;
  expression  : TExpr;
  myArray     : TArray;
  myRecord    : TRecord;
  letters     : TLetters;

begin
  // Assign values to these types
  firstName       := 'Neil';
  lastName        := 'Moffatt';
  temperature     := Cold;
  expression      := 10;
  myArray[1]      := 5;
  myRecord.header := 'data file';
  letters         := ['F'..'Q'];
end;
Show full unit code
  The program runs with no output
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page