Icon Casting Types

Casting is the process of converting a target value of one type to another type in order to use the value in a different type context. This is accomplished by enclosing the target value with the target type name and parentheses:

<Type Name>(<Target Value>)

There are type compatibility rules that determine whether a particular cast operation is valid, and invalid cast attempts will result in a compiler error. The following table details the various types and their valid target cast types:

Source TypeValid Target Types
IntegerInteger
Boolean
Double
DateTime
Enumeration
DoubleDouble
StringString
Char
CharChar
String
DateTimeDateTime
Integer
BooleanBoolean
Integer
EnumerationEnumeration
Integer
Class Instance
Class Type
Any same class type or ancestor class type

Casting is particularly useful for functions or procedures that accept a parameter of a base class type, but need to act on the various descendant class types in specific ways. For example, the following code shows how one would use the is operator to determine if the parameter is of the TComponent type and, if so, displays its name:

procedure DisplayName(Value: TObject);
begin
   if (Value is TComponent) then
      ShowMessage(TComponent(Value).Name);
end;
Image