?
Solved

Defining property which calls another property

Posted on 2005-04-04
10
Medium Priority
?
160 Views
Last Modified: 2013-11-23
Is it somehow possible to define a property that calls a property of a field in the class:

Example:

TClass1:
  private
    field1 : Integer;
  public
    property f : Integer read field1 write field1;
//other properties
  end;

TClass2:
  private
    field2 : TClass1;
  public
    property f: Integer read field2.f write field2.f;      //  How can I do this
  end;


TClass1 cannot be changed and I want to avoid to write new getter and setter methods in the TClass2. I would prefere to do this by declaring a new property only.

Thanks for your help.
 
0
Comment
Question by:maverick65
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 19

Expert Comment

by:Barthax
ID: 13698716
Yes it is - set up the property of TClass2 to use a method as follows:

TClass1:
  private
    field1 : Integer;
  public
    property f : Integer read field1 write field1;
//other properties
  end;

TClass2:
  private
    function ReadF: Integer;
    procedure WriteF(AInt : Integer);
    field2 : TClass1;
  public
    property f: Integer read ReadF write WriteF;      //  How can I do this
  end;

implementation

function TClass2.ReadF : Integer;
begin
  result := TClass1.f;
end;

procedure TClass2.ReadF(AInt : Integer);
begin
  TClass1.f := AInt;
end;


0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13698893
wouldn't it be more like...:

TClass1:
  private
    field1 : Integer;
  public
    property f : Integer read field1 write field1;
//other properties
  end;

TClass2:
  private
    field2 : TClass1;
  public
    property f: Integer read field2.f write field2.f;      //  How can I do this
  end;

TClass2:
  private
    function ReadF: Integer;
    procedure WriteF(Value : Integer);
    field2 : TClass1;
  public
    property f: Integer read ReadF write WriteF;      //  How can I do this
  end;

implementation

function TClass2.ReadF : Integer;
begin
  result := field2.f;
end;

procedure TClass2.ReadF(Value : Integer);
begin
  field2.f := Value;
end;

0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13698917
oops... ignore the first TClass2 declaration... argh... and the code by Barthax  is wrong too... here's the corrected version:

TClass1:
  private
    field1 : Integer;
  public
    property f : Integer read field1 write field1;
//other properties
  end;

TClass2:
  private
    function ReadF: Integer;
    procedure WriteF(Value : Integer);
    field2 : TClass1;
  public
    property f: Integer read ReadF write WriteF;      //  How can I do this
  end;

implementation

function TClass2.ReadF : Integer;
begin
  result := field2.f;
end;

procedure TClass2.WriteF(Value : Integer);
begin
  field2.f := Value;
end;

of course, Field2 needs to be instantiated (most likely on the constructor)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:maverick65
ID: 13698934
As I already mentioned I do not want to introduce getter and setter methods.

I have quite a lot of properties that I have to define this way. When I have to add a getter and setter method for each property this is a lot more work (11 Code Lines) than simply defining a property(1 Code Line). And I case of a change I would have to change it only at one place in the code instead of 4.
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 2000 total points
ID: 13698941
...never mind... didn't read this:
"I want to avoid to write new getter and setter methods in the TClass2"

in that case, I don't think that's possible just by declaring it
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13700222
maybe ?

your class1 should be derived from TPersistent

your class 2 looks then like

TClass2:
  private
    function ReadF: Integer;
    procedure WriteF(Value : Integer);
    field2 : TClass1;
  public
    property f: TClass1 read Read field2;      //  How can I do this
  end

now you could access the property like

Class2Inst.f.f := Whatever;

just as an idea

meikl ;-)
0
 
LVL 19

Expert Comment

by:Barthax
ID: 13702745
Sorry, I missed "I want to avoid to write new getter and setter methods in the TClass2" too. :(

Not possible in my experience. :(

Personally, I write the property line & then hit CTRL+ALT+C & Delphi will create all but the "result := field2.f;" and "field2.f := Value;" - the function/procedure will appear as BlackTigerX shows (I did mine from memory). :)

maverick65: What's the end aim of all this - there may be a better way than masses of properties to one object then used by another.  kretzschmar already points out one way: making the inside object available externally.
0
 
LVL 10

Expert Comment

by:Jacco
ID: 15506145
Probably of no use but if you let TClass2 descend from TClass1 the property f is automatically available.

type
  TClass1 = class
  private
    field1 : Integer;
  public
    property f : Integer read field1 write field1;
  end;

  TClass2 = class(TClass1)
  end;

I found out lately that this kind of code is possible:

type
  TClass1 = record
    f : Integer;
  end;

  TClass2 = class
  private
    field2 : TClass1;
  public
    property f: Integer read field2.f write field2.f;  
  end;

You already said you cannot change TClass1 so this is no use as well.

Regards Jacco
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16374465
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: BlackTigerX {http:#13698941}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

cwwkie
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question