Comming from a C# background I’m trying to learn Delphi.
I encounter an Access violation when in my form I press a button that creates a TLight instance.
Wherever I try to access my private FState I get an access violation.
What am I missing?
unit Light;
interface
uses sysUtils;
type
TLightStates = (Red, Orange, Green);
type
TLight = class
private
Fstate : TLightStates;
published
Constructor Create(); overload;
procedure SetState(const Value: TLightStates);
Property State : TLightStates
read Fstate
write SetState;
end;
implementation
{ TLight }
constructor TLight.Create;
begin
Fstate := TLightStates.Red;
end;
procedure TLight.SetState(const Value: TLightStates);
begin
Fstate := Value;
end;
end.
Did you create an object in your test code where the property State is set?
Looking at the code this should work correctly.
One other thing: why did you specify the contructor with overload: you derive from TObject and it does not have a virtual constructor, so overload should not be specified here.