Normally, a function returns a normal data type, like a string, eg:
Function getInitials(sName,sSurname:string) :string
Begin
Result := sName [1]+sSurname [1]
End
And is called like this:
var s: string;
s := getInitials (John, Wick);
// s will have the value 'JW'
As you can see, the Result
is assigned a value using :=
.
What if we need to allow the end user to create a CUSTOM OnClick
event for a dynamic button?
Var
BtnNEW: TButton;
Procedure MakeButtonInRuntime;
Const
sLine = 'formColor=Red'
Begin
BtnNEW := TButton.Create(frmHome);
With btnNew DO
Begin
Name := btnRed;
OnClick := MakeOnClick(sLine);
End
End
Function MakeOnClick(doThis: string) : procedure;
Begin
Result := (frmHome.color := clRed);
End;
So, the plan is that once the program runs, a brand new TButton
is made.
The button then gets a custom OnClick
(from a function) based from sLine
. In this case, it will give the button the ability to change the form's colors once clicked.
The problem is that the compiler doesn't work with this code as the syntax is not getting a normal data type and instead a procedure.
John
versus'John'
).