Var AnObject : TAnObject; begin AnObject := TAnObject.Create; ANobject.AMethod;
The following code is wrong:
The compiler will produce a warning:
Warning: An inherited method is hidden by OBJCHILD.MYPROCThe compiler will compile it, but using Inherited can produce strange effects.
The correct declaration is as follows:
This will compile and run without warnings or errors.
Message methods that are declared with an integer constant can take only one
var argument (typed or not):
The method implementation of a message function is no different from an
ordinary method. It is also possible to call a message method directly,
but you should not do this. Instead use the TObject.Dispatch method.
The TOBject.Dispatch method can be used to call a message
handler. It is declared in the system unit will accept a var parameter
which must have at the first position a cardinal with the message ID that
should be called. For example:
In this example, the Dispatch method will look at the object and all
it's ancestors (starting at the object, and searching up the class tree),
to see if a message method with message MSGID has been
declared. If such a method is found, it is called, and passed the
Msg parameter.
If no such method is found, DefaultHandler is called.
DefaultHandler is a virtual method of TObject that doesn't do
anything, but which can be overridden to provide any processing you might
need. DefaultHandler is declared as follows:
In addition to the message method with a Integer identifier,
Free Pascal also supports a messae method with a string identifier:
The working of the string message handler is the same as the ordinary integer message handler:
The TOBject.DispatchStr method can be used to call a message
handler. It is declared in the system unit and will accept one parameter
which must have at the first position a cardinal with the message ID that
should be called. For example:
In this example, the DispatchStr method will look at the object and
all it's ancestors (starting at the object, and searching up the class tree),
to see if a message method with message MsgStr has been
declared. If such a method is found, it is called, and passed the
Msg parameter.
If no such method is found, DefaultHandlerStr is called.
DefaultHandlerStr is a virtual method of TObject that doesn't do
anything, but which can be overridden to provide any processing you might
need. DefaultHandlerStr is declared as follows:
In addition to this mechanism, a string message method accepts a self
parameter:
When encountering such a method, the compiler will generate code that loads
the Self parameter into the object instance pointer. The result of
this is that it is possible to pass Self as a parameter to such a
method.
remark: The type of the Self parameter must be of the same class as the class you define the method for.