Re: Type casting using type name
From: Tom de Neef (tdeneef_at_qolor.nl)
Date: Tue, 28 Sep 2004 11:25:06 +0200
"TK Boyd" <email@example.com>
> Is the use of TGridRect above a means of type casting? It seems that
> TGridRect is being used almost like a function to turn a type TRect
> value into a TGridRect value. Is there a better way to describe,
> explain, what is going on? (I'm happy with "type casting" if that's
> Are there other times when this "type name used as function" trick is
Yes, this is a typecast. NewType(source) tells the compiler to interpret
Source as data of type NewType. It will work only if such a translation is
It is frequently used when using pointers or Tobjects. If you use a
TstringList for instance, you can assign an object that is linked to each
string in the list. So you'd say something like
stringlist.objects[i]:=MyObject; MyObject can be any descendant of Tobject,
normally an instance of a class. But the reverse,
MyObject:=stringlist.objects[k] will not work because the compiler can not
ascertain that this is a good thing to do. But you can. So you tell the
compiler via a typecast: MyObject:=TmyObject(stringlist.objects[k]).
Another example would be char(32), which will cast a byte to a character.
Related to this is the AS operator.
Check Help - Typecasts.