1 {2 Sometimes when accessing properties you need to include extra code just3 to make sure that the object exists. For example, consider the class:4 }5 6 type c00G1026 = class (TObject)
7 private8 ...
9 iIdG : word;
10 ...
11 public12 ...
13 property aIdG : word
14 read iIdG;
15 ...
16 end;
17 18 {19 When you use an object belonging to this class, you need to check 20 whether the object exists before accessing its properties:21 }22 23 var wObj : c00G1026;
24 var wIdG : word;
25 26 ...
27 28 if (wObj <> nil) and (wObj.aIdG > 9) then29 ...
30 31 if wObj <> nilthen32 wIdG := wObj.aIdG
33 else34 wIdG := 0;
35 36 ...
37 38 {39 If you use a property access method, you can avoid this extra code 40 by checking for a non-nil object in the method:41 }42 43 type c00G1026 = class (TObject)
44 private45 ...
46 iIdG : word;
47 function mhGetIdG_S () : word;
48 ...
49 public50 ...
51 property aIdG_S : word
52 read mhGetIdG_S;
53 ...
54 end;
55 56 function c00G1026.mhGetIdG_S () : word;
57 begin58 if Self <> nilthen59 Result := iIdG
60 else61 Result := 0
62 end;
63 64 {65 Then you can simplify code which uses the class:66 }67 68 var wObj : c00G1026;
69 var wIdG : word;
70 71 ...
72 73 if wObj.aIdG_S > 9 then74 ...
75 76 wIdG := wObj.aIdG_S;
77 78 ...