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.
The property accessor is the expression following the
read. If your property was writeable, then the expression following thewritewould 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
fTestis not an instance field.And that is correct. It is a class field.
I guess you don’t mean for
fTestto be a class field. You need to write the class like this:Or perhaps:
I favour the former as it gives a much clearer distinction between class and instance fields.