Is it possible to pass an object function as a parameter in a procedure rather than passing the whole object?
I have a record definition with a function defined as a public class parameter such as:
TMyRecord = record
public
class function New(const a, b: Integer): TMyRecord; static;
function GiveMeAValue(inputValue: integer): Single;
public
a, b: Integer;
end;
The function could be something like:
function TMyRecord.GiveMeAValue(inputValue: Integer): Single;
begin
RESULT := inputValue/(self.a + self.b);
end;
I then wish to define a procedure that calls on the class function GiveMeAValue
but I don't want to pass it the whole record. Can I do something like this, for example:
Procedure DoSomething(var1: Single; var2, var3: Integer, ?TMyRecord.GiveMeAValue?);
begin
var1 = ?TMyRecord.GiveMeAValue?(var2 + var3);
//Do Some Other Stuff
end;
If so then how would I correctly pass the function as a procedure parameter?
You can define a new type for the function like
TGiveMeAValue= function(inputValue: integer): Single of object;// this definition works fine for methods for records.
then define the method DoSomething
Procedure DoSomething(var1: Single; var2, var3: Integer;GiveMeAValue: TGiveMeAValue);
begin
writeln(GiveMeAValue(var2 + var3));
end;
And use like so
var
L : TMyRecord;
begin
l.a:=4;
l.b:=1;
DoSomething(1, 20, 5, L.GiveMeAValue);
end;