Recent

Author Topic: Why no access to address of Label.Caption?  (Read 2177 times)

Prefect

  • Newbie
  • Posts: 2
Why no access to address of Label.Caption?
« on: May 26, 2017, 01:46:17 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   testCaption:TCaption;
  4.   testLabel:TLabel;
  5.   ptestCaption:^TCaption;
  6.  
  7.   testMemo:TMemo;
  8.   testStrings:TStrings;
  9.   ptestStrings:^TStrings;
  10.  
  11. begin
  12.  
  13.   ptestStrings:=@testStrings;
  14.   ptestStrings:=@(testMemo.Lines);
  15.  
  16.   ptestCaption:=@testCaption;
  17.   ptestCaption:=@(testLabel.Caption); // Error: Variable identifier expected
  18.  

sky_khan

  • Guest
Re: Why no access to address of Label.Caption?
« Reply #1 on: May 26, 2017, 03:07:22 pm »
Lines is member variable, Caption needs function calling so it is not a variable.

Code: Pascal  [Select][+][-]
  1. property Lines: TStrings read FLines write SetLines;
  2. property Caption: TCaption read GetText write SetText stored IsCaptionStored;
  3.  

Prefect

  • Newbie
  • Posts: 2
Re: Why no access to address of Label.Caption?
« Reply #2 on: May 26, 2017, 05:47:52 pm »
Function (and procedure) address can be accessed through using Pointer type.
So why not for a property read function?

Code: Pascal  [Select][+][-]
  1.   procedure testProcedure;
  2.     var _result:integer;
  3.   begin
  4.     _result:=0;
  5.   end;
  6.  
  7.   function testFunction:integer;
  8.   begin
  9.     result:=0;
  10.   end;  
  11. var
  12.   testCaption:TCaption;
  13.   testLabel:TLabel;
  14.  
  15.   testMemo:TMemo;
  16.   testStrings:TStrings;
  17.  
  18.   ptestInt:PInteger;
  19.   pPointer:Pointer;
  20. begin
  21.  
  22.   pPointer:=@testStrings;
  23.   pPointer:=@(testMemo.Lines);
  24.  
  25.   ptestInt:=@testFunction; // Error: Incompatible types: got "<address of function:LongInt is nested;Register>" expected "PInteger"
  26.   pPointer:=@testProcedure;
  27.   pPointer:=@testFunction;
  28.  
  29.   pPointer:=@testCaption;
  30.   pPointer:=@(testLabel.Caption); // Error: Variable identifier expected  
  31.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Why no access to address of Label.Caption?
« Reply #3 on: May 26, 2017, 08:12:18 pm »
Property getters are not simple functions. Class methods have first a reference to the class instance (Self) which the compiler needs before it can determine the address of the method in question (a different instance of the same class would have methods at different addresses in memory).

The property keyword does not declare a variable. Properties are not variables but a syntax extension to the original Pascal, a form of indirection giving controlled access to normally inaccessible routines or variables. However in the simple case that a property is an alias for a simple variable (a field somewhere in the class declaration) the @ operator can be used.

I fail to imagine a situation in which knowledge of the address of a private method is really useful, but perhaps such a situation arises for you.

sky_khan

  • Guest
Re: Why no access to address of Label.Caption?
« Reply #4 on: May 26, 2017, 09:17:24 pm »
If you really need to do this, see below but you should know this is much more slower than normal function call.

Code: Pascal  [Select][+][-]
  1. uses
  2.   typInfo;
  3.  
  4. type
  5.   TCaptionFunc = function:string of object;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   info : PPropInfo;
  10.   captionGetter : TMethod;
  11. begin
  12.   info:=getPropInfo(self,'caption');
  13.   if Assigned(info) then
  14.   begin
  15.      captionGetter.Code:=info^.GetProc;
  16.      captionGetter.Data:=self;
  17.      ShowMessage('My caption is '+TCaptionFunc(captionGetter)());
  18.   end;
  19. end;
  20.  

 

TinyPortal © 2005-2018