Articles   Members Online:
-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
How to use the power of accessors 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
07-Nov-02
Category
Algorithm
Language
Delphi 3.x
Views
94
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Pieter Valentijn

Making properties in your objects and using the accesors to do something. 

Answer:

Property accessors are great.  They give you the power to do stuff like initialize 
an object or produce data that are dependent on the object's other properties. 

You can make properties by declaring them in your interface section and then 
pressing Ctrl+Shift+'c'  it wil make a private variable called Fsomeproperty and a 
set methode called SetSomeProperty. 

In this example i made an object that has a property that holds an other object and 
on the Read of this property i'll check whether it's not nil so i can create it on 
runtime. 

You can also check if a value is within it's bounds on the Property's Set Accessor 

1   type
2     TMySubObject = class
3   
4     end;
5   
6     TMyObject = class
7     private
8       fMySubObject: TMySubObject;
9       FASecondNum: Integer;
10      FAFirstnum: Integer;
11      function getMySubObject: TMySubObject;
12      function GetTotaalOfFirstAndSecondNum: integer;
13      procedure SetFirstnum(const Value: Integer);
14    public
15  
16      property MySubObject: TMySubObject read getMySubObject;
17      property AFirstnum: Integer read FAFirstnum write SetFirstnum;
18      property ASecondNum: Integer read FASecondNum write FAFirstnum;
19      property TotaalOfFirstAndSecondNum: integer read
20        GetTotaalOfFirstAndSecondNum;
21    end;
22  
23  implementation
24  
25  {$R *.DFM}
26  
27  { TMyObject }
28  
29  function TMyObject.getMySubObject: TMySubObject;
30  begin
31    // i check here to see if its assigned
32    if not Assigned(fMySubObject) then
33      fMySubObject := TMySubObject.Create;
34    Result := fMySubObject;
35  end;
36  
37  function TMyObject.GetTotaalOfFirstAndSecondNum: integer;
38  begin
39    Result := FAFirstnum + FASecondNum;
40  end;
41  
42  procedure TMyObject.SetFirstnum(const Value: Integer);
43  begin
44    // here on the set u can check bounds :) .
45    if (Value > 0) and (Value < 1000) then
46      FAFirstnum := Value
47    else
48      raise Exception.Create('Number out of bounds');
49  end;


			
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