Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə72/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   68   69   70   71   72   73   74   75   ...   294
DelphiLanguageGuide

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
Char(I)
Boolean(Count)
TSomeDefinedType(MyVariable)
          
Variable typecasts can appear on either side of an assignment statement. Thus
56


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
  F: Func;
  P: Pointer;
  N: Integer;
you can make the following assignments.
F := Func(P);     { Assign procedural value in P to F }
Func(P) := F;     { Assign procedural value in F to P }
@F := P;          { Assign pointer value in P to F }
P := @F;          { Assign pointer value in F to P }
N := F(N);        { Call function via F }
N := Func(P)(N);  { Call function via P }
Variable typecasts can also be followed by qualifiers, as illustrated in the following example.
type
  TByteRec = record
     Lo, Hi: Byte;
  end;
  TWordRec = record
     Low, High: Word;
  end;
var
  B: Byte;
  W: Word;
  L: Longint;
  P: Pointer;
begin
  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)^;
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.
57


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.
58



Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   68   69   70   71   72   73   74   75   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin