Here is your example that works ...
program Project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes
{ you can add units after this };
Type TWatchDog = Class (TObject)
Private
FOnChanged : TNotifyEvent;
FMYData : Boolean;
Procedure SetMyData (Value: Boolean);
Public
procedure AfterConstruction; override;
Property OnChanged : TNotifyEvent Read FOnchanged write FOnChanged ;
Property MyData : Boolean Read FMyData Write SetMyData ;
Procedure ProcessEvent(Sender: TObject);
end;
Var WatchDog : TWatchDog ;
procedure TWatchDog.AfterConstruction;
begin
inherited;
// Intialization
// It's a good habit to always initialize your variables
FMyData := False;
end;
Procedure TWatchDog.SetMyData(Value: Boolean);
begin
If MyData = Value Then Exit;
FMyData := Value;
if Assigned(FOnChanged) then
FOnChanged(Self);
end;
Procedure TWatchDog.ProcessEvent(Sender: TObject);
begin
//Do some Stuff
writeln('Change event has been fired');
readln;
End;
begin
WatchDog := TWatchDog.Create;
WatchDog.OnChanged := @WatchDog.ProcessEvent;
writeln('set to false');
// MyData is already false (default value),
// so change event will not triggerd
Watchdog.MyData := false;
readln;
writeln('set to True');
Watchdog.MyData := true;
readln;
WatchDog.Free;
end.
But in your example, you have ProcessEvent procedure in the same class so you don't
need event at all, you can simply directly call your ProcessEvent procedure.
Here is an example:
program Project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes
{ you can add units after this };
Type TWatchDog = Class (TObject)
Private
FOnChanged : TNotifyEvent;
FMYData : Boolean;
Procedure SetMyData (Value: Boolean);
Public
procedure AfterConstruction; override;
Property OnChanged : TNotifyEvent Read FOnchanged write FOnChanged ;
Property MyData : Boolean Read FMyData Write SetMyData ;
Procedure ProcessEvent;
end;
Var WatchDog : TWatchDog ;
procedure TWatchDog.AfterConstruction;
begin
inherited;
// Intialization
// It's a good habit to always initialize your variables
FMyData := False;
end;
Procedure TWatchDog.SetMyData(Value: Boolean);
begin
If MyData = Value Then Exit;
FMyData := Value;
ProcessEvent;
end;
Procedure TWatchDog.ProcessEvent;
begin
//Do some Stuff
writeln('Change event has been fired');
readln;
End;
begin
WatchDog := TWatchDog.Create;
writeln('set to false');
// MyData is already false (default value),
// so change event will not triggerd
Watchdog.MyData := false;
readln;
writeln('set to True');
Watchdog.MyData := true;
readln;
WatchDog.Free;
end.
Here is some real-world example that makes sense,
where you have an event handler in another class.
program Project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes
{ you can add units after this };
Type TWatchDog = Class(TObject)
Private
FOnChanged : TNotifyEvent;
FMYData : Boolean;
FIsMyDataInitInitalized: Boolean;
Procedure SetMyData (Value: Boolean);
Public
procedure AfterConstruction; override;
Property OnChanged : TNotifyEvent Read FOnchanged write FOnChanged ;
Property MyData : Boolean Read FMyData Write SetMyData ;
end;
Type TSomeWorkingClass = Class(TObject)
public
Procedure ProcessEvent(Sender: TObject);
end;
Var WatchDog : TWatchDog;
Var WorkingClass : TSomeWorkingClass;
procedure TWatchDog.AfterConstruction;
begin
inherited;
FMyData := False;
FIsMyDataInitInitalized := False;
end;
Procedure TWatchDog.SetMyData(Value: Boolean);
begin
If (MyData = Value) and (FIsMyDataInitInitalized) Then Exit;
FMyData := Value;
FIsMyDataInitInitalized := TRUE;
If Assigned(FOnChanged) Then
Self.OnChanged(Self);
end;
Procedure TSomeWorkingClass.ProcessEvent(Sender: TObject);
begin
//Do some Stuff
writeln('Change event has been fired');
end;
begin
WatchDog := TWatchDog.Create;
WorkingClass := TSomeWorkingClass.Create;
Try
WatchDog.OnChanged := @WorkingClass.ProcessEvent;
writeln('set to false');
Watchdog.MyData := false;
readln;
writeln('set to True');
Watchdog.MyData := true;
readln;
finally
WatchDog.Free;
WorkingClass.Free;
end;
end.