Delphi> Please explain this: type ... object procedure

I came across some new code for me ...

I have never seen declarations of the type of an object type , and I just do not see the paragraph. Why couldn't a developer just save a field like Boolean?

interface type TFinishedCaptureEvent = procedure(AFinished: Boolean) of object; TFrameCard = class(TFrame) ... private FOnFinishedCapture: TFinishedCaptureEvent; procedure DoUpdateMessage(AMessageType: TMessageType); public property OnFinishedCapture: TFinishedCaptureEvent read FOnFinishedCapture write FOnFinishedCapture; end; implementation ... procedure TFrameCard.DoUpdateMessage(AMessageType: TMessageType); begin if Assigned(FOnFinishedCapture) then FOnFinishedCapture(False); ... end; end. 
+6
delphi
source share
3 answers

A procedure of object is a reference to a procedure for procedures contained in instances of a class. When calling procedures that are members of a class, the impict Self reference must be passed with other parameters. Using procedure of object tells the compiler to save the Self reference with the address of the procedure inside the procedure reference, so that when the procedure is called using the procedure, the Self reference will be automatically passed.

In the above code snippet, TFinishedCaptureEvent is defined as an object procedure, which means that any variables created from its type will contain 2 values: the Self value and the address of the procedure. When this variable is assigned, in particular when the assignment is inside the class, the compiler automatically sets the Self value inside this variable to an instance of the class that contains the procedure assigned to the variable. When a variable is called ( FOnFinishedCapture(False) ), the compiler automatically passes the correct Self value back to the procedure assigned to this variable.

+12
source share

Let me break this down into two so that they are easier to understand. Firstly, procedure(AFinished: Boolean) not a boolean variable, it is a reference to a procedure that takes a boolean value as a parameter. This is basically the heading of a procedure, except for without a procedure name, because it is just a type definition. Any procedure matching this signature can be assigned to this variable.

The of object means that this is not just a reference to a procedure, but a reference to a method; it must belong to the object. The compiler needs to know the difference so that it can store the self pointer for the object along with the procedure pointer so that it can be called correctly, as other posters pointed out.

Basically, this is an announcement of a new event handler, and it is a fairly common pattern in Delphi. This is the same thing that VCL does everywhere. When you create a button and assign an OnClick handler, it should be procedure (Sender: TObject) of object; . Your form gives the button a link to the method, referring to itself and the event handler procedure, and then the button uses this information to call the handler in the form when someone clicks on it.

This code does the same. This provides a way to notify any external object when DoUpdateMessage starts, using the standard Delphi idiom for event notification.

+16
source share

I do not understand how you feel about the logical field.

But TFinishedCaptureEvent = procedure(AFinished: Boolean) of object declares the type of the delegate / method pointer that is used for events. This is a record that contains a pointer to self and a pointer to a function. When you call the delegate, the function is called with self passed as a parameter to the function.

+1
source share

All Articles