When compiling this unit:
unit Test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
class var StartDate, EndDate: TDateTime; // Line 12
fTest: TNotifyEvent;
public
property OnTest: TNotifyEvent read fTest; // Line 15.
end;
implementation
{$R *.dfm}
end.
I get the following compiler error:
[DCC Error] Test.pas(15): E2356 Property accessor must be an instance field or method
But if I comment out line 12 it compiles fine. Can someone explain why? I need the dates as class variables to store a date interval.
That error should be self-explanatory. Let's try to de-construct it.
Property accessor must be an instance field or method.
The property accessor is the expression following the read
. If your property was writeable, then the expression following the write
would also be a property accessor.
Therefore, in your code the property accessor is fTest
.
An instance field is a normal field of the class. So, class fields do not qualify. Similarly, an instance method is a plain method of the class. A class method is not an instance method. In fact, any method that is not a class method are instance methods.
The error therefore indicates that fTest
is not an instance field.
And that is correct. It is a class field.
private
class var StartDate, EndDate: TDateTime;
fTest: TNotifyEvent; // class var applies to fTest also
I guess you don't mean for fTest
to be a class field. You need to write the class like this:
TForm1 = class(TForm)
private
class var StartDate, EndDate: TDateTime;
private
fTest: TNotifyEvent;
public
property OnTest: TNotifyEvent read fTest;
end;
Or perhaps:
TForm1 = class(TForm)
private
class var
StartDate, EndDate: TDateTime;
var
fTest: TNotifyEvent;
public
property OnTest: TNotifyEvent read fTest;
end;
I favour the former as it gives a much clearer distinction between class and instance fields.