Recent

Author Topic: [Solved] Class Property problems  (Read 2641 times)

iru

  • Sr. Member
  • ****
  • Posts: 321
[Solved] Class Property problems
« on: January 12, 2019, 12:06:18 am »
Gentlefolk,

I have a problem with a 'property' when trying to access 'private' data on a class definition.

Environment: Linux Mint 19, Laz 1.8.4.

The code is:
  TEventsDM = class(TDataModule)
    ..
  private
    fCurrentEvent : tDataResults;
    Procedure SetCurr(InData : tDataResults);
    Function  GetCurr : tDataResults;
  public
    constructor Create(Sender: TObject);
 
    property SetCurrent : tDataResults read GetCurr write SetCurr;
    property GetCurrent : tDataResults read GetCurr write SetCurr;
  end;

  EventsDM : TEventsDM;

And SetCurr and GetCurr code.

The above code compiles clean but the compiler complains about the following

  EventsDM.SetCurrent(...);  with  '(found, expecting ;'.

Remove the (...) and the code compiles clean but when stepped through in the debugger executes the code for GetCurrent.

If I try and tidy up the property/s to

    property SetCurrent : tDataResults write SetCurr;
    property GetCurrent : tDataResults read GetCurr; 

The compile fails reporting    'No member is provided to access property'.

Any help appreciated, Ian.


               
« Last Edit: January 12, 2019, 09:42:06 am by iru »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Class Property problems
« Reply #1 on: January 12, 2019, 12:33:33 am »
I can't see anything wrong with the code (ommiting the fact that two properties have the same getter and setter). It compiles well. However, naming of properties Get... and Set... is confusing.

And this:
Code: Pascal  [Select][+][-]
  1. property SetCurrent : tDataResults write SetCurr;
  2.  
is nonsense, or at least very unusual. A write-only property.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Class Property problems
« Reply #2 on: January 12, 2019, 12:47:13 am »
  TEventsDM = class(TDataModule)
    ..
  private
    fCurrentEvent : tDataResults;
    Procedure SetCurr(InData : tDataResults);
    Function  GetCurr : tDataResults;
  public
    constructor Create(Sender: TObject);
 
    property SetCurrent : tDataResults read GetCurr write SetCurr;
    property GetCurrent : tDataResults read GetCurr write SetCurr;
  end;

  EventsDM : TEventsDM;

You just need to clean up the property definition.

Code: [Select]
  TEventsDM = class(TDataModule)
    fCurrentEvent: tDataResults;
  private
    procedure SetCurrentEvent(AData: tDataResults);
    function  GetCurrentEvent: tDataResults;
  public
    constructor Create(Sender: TObject);
    property CurrentEvent: tDataResults read GetCurrentEvent write SetCurrentEvent;
  end;
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Class Property problems
« Reply #3 on: January 12, 2019, 01:01:01 am »
Its strange that compiler is getting confused on that Hmm....

 Also I don't know if you are handling the inherited create or not?

 are you calling the inherited create somewhere ?


The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Class Property problems
« Reply #4 on: January 12, 2019, 01:08:21 am »
The problem with your code is that you're trying to access a property as if it where a function and that not only can't be done but doesn't make sense either: that's what the getter/setter are for. Using the (more correct) format:

Code: Pascal  [Select][+][-]
  1. TEventsDM = class(TDataModule)
  2.     ..
  3.   private
  4.     fCurrentEvent : tDataResults;
  5.     Procedure SetCurr(InData : tDataResults);
  6.     Function  GetCurr : tDataResults;
  7.   public
  8.     constructor Create(Sender: TObject);
  9.  
  10.     property Current : tDataResults read GetCurr write SetCurr;
  11.   end;
  12.  
  13.   EventsDM : TEventsDM;

when your other code reads or writes EventsDM.Current, as in:
Code: Pascal  [Select][+][-]
  1. MyDR := EventsDM.Current;
  2. {...more code ...}
  3. EventsDM.Current := MyDR;
the compiler generates calls to GetCurr and SetCurr transparently, without any more intervention from yourself.

Maybe you need to give a look-through to the Language Reference :)

ETA: I forgot! Regarding this:
Quote
If I try and tidy up the property/s to
    property SetCurrent : tDataResults write SetCurr;
    property GetCurrent : tDataResults read GetCurr;


The compile fails reporting: 'No member is provided to access property'.

In which line does it fails? I have just tested with this code:
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     {...standard controls & handlers here ...}
  3.   private
  4.     procedure SetWriteMe(AValue: String);
  5.     function GetReadMe: String;
  6.   public
  7.     property WriteMe: String write SetWriteMe;
  8.     property ReadMe: String read GetReadMe;
  9.     {There is "proper" code  for SetWriteMe and GetReadMe:
  10.      ShowMessage for WriteMe and InputBox for ReadMe}
  11.   end;
and it compiles (and works) alright. It may or may not make sense but read-only and write-only properties work ok.
« Last Edit: January 12, 2019, 02:18:05 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

iru

  • Sr. Member
  • ****
  • Posts: 321
[Solved] Re: Class Property problems
« Reply #5 on: January 12, 2019, 09:41:25 am »
Gentlefolk,

Thank you all for your comments.
I think that the comment and example code by lucamar covers things very well.

The code that failed to compile was 'EventsDM.SetCurrent(...);' where I was trying to use the property.

So I will redo a few things and battle on.

Thanks once again, Ian.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: [Solved] Class Property problems
« Reply #6 on: January 12, 2019, 06:00:09 pm »
Aside from unexplained naming and functional logic, formally you're trying to use write property as a procedure call, while it should be used as "L-Value", i.e. with assignment operator.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018