Variable Typecasts

You can cast any variable to any type, provided their sizes are the same and you do not mix integers with reals. (To convert numeric types, rely on standard functions like Int and Trunc.) Examples of variable typecasts include




Variable typecasts can appear on either side of an assignment statement. Thus var MyChar: char;

Shortint(MyChar) := 122; assigns the character z (ASCII 122) to MyChar.

You can cast variables to a procedural type. For example, given the declarations type Func = function(X: Integer): Integer; var

you can make the following assignments.

Assign procedural value in P to F Assign procedural value in F to P

Assign pointer value in P Assign pointer value in F Call function via F } Call function via P }

to to

Variable typecasts can also be followed by qualifiers, as illustrated in the following example.


TByteRec = record

TWordRec = record

L: Longint;

P: Pointer;


W := $1234; B := TByteRec(W).Lo; TByteRec(W).Hi := 0; L := $1234567; W := TWordRec(L).Low; B := TByteRec(TWordRec(L).Low).Hi; B := PByte(L)A; end;

In this example, TByteRec is used to access the low- and high-order bytes of a word, and TWordRec to access the low- and high-order words of a long integer. You could call the predefined functions Lo and Hi for the same purpose, but a variable typecast has the advantage that it can be used on the left side of an assignment statement.

For information about typecasting pointers, see Pointers and pointer types. For information about casting class and interface types, see The as operator and Interface typecasts.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment