Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Defining property which calls another property

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
maverick65
Asked:
maverick65
1 Solution
 
BarthaxCommented:
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
 
BlackTigerXCommented:
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
 
BlackTigerXCommented:
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
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.

 
maverick65Author Commented:
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
 
BlackTigerXCommented:
...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
 
kretzschmarCommented:
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
 
BarthaxCommented:
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
 
JaccoCommented:
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
 
cwwkieCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now