Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Properties and nil objects Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
24-Oct-04
Category
N/A
Language
Delphi All Versions
Views
251
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Crann, Ian
Reference URL:
			1   {
2     Sometimes when accessing properties you need to include extra code just
3     to make sure that the object exists.  For example, consider the class:
4   }
5   
6     type c00G1026 = class (TObject)
7       private
8          ...
9         iIdG : word;
10         ...
11      public
12         ...
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) then
29     ...
30  
31    if wObj <> nil then
32      wIdG := wObj.aIdG
33    else
34      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      private
45         ...
46        iIdG : word;
47        function mhGetIdG_S () : word;
48         ...
49      public
50         ...
51        property aIdG_S : word
52                 read mhGetIdG_S;
53         ...
54    end;
55  
56    function c00G1026.mhGetIdG_S () : word;
57      begin
58        if Self <> nil then
59          Result := iIdG
60        else
61          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 then
74     ...
75  
76    wIdG := wObj.aIdG_S;
77  
78     ...    


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC