Extra 3
Extra 3
you write the method name, which is a combination of the record type name and the
actual record name and on the fact you can directly refer to the fields and the other
methods of the record directly, with no need to write the name of the record:
procedure TMyRecord.SetValue (NewString: string);
begin
Name := NewString;
end;
note While it might seem tedious having to write the definition of the method first and its full declara-
tion next, you can use the Ctrl+Shift+C combination in the IDE editor to generate one from the
other automatically. Also you can use the Ctrl+Shift+Up/Down Arrow keys to move from a
method declaration to the corresponding definition and vice verse.
note Again, the address of operator is shortly covered at the end of this chapter in the (advanced) sec-
tion titled “What About Pointers?”
This is how the calling code is translated, but how can the actual method call refer
and use this hidden parameter? By implicitly using a special keyword called self. So
the method's code could be written as:
procedure TMyRecord.SetValue (NewString: string);
begin
self.Name := NewString;
end;
While this code compiles, it makes little sense to use self explicitly, unless you need
to refer to the record as a whole, for example passing the record as parameter to
another function. This happens more frequently with classes, which have the same
exact hidden parameter for methods and the same self keyword.
One situation in which using an explicit self parameter can make the code more
readable (even if it is not required) is when you are manipulating a second data
structure of the same type, as in case you are testing a value from another instance:
function TMyRecord.IsSameName (ARecord: TMyRecord): Boolean;
begin
Result := (self.Name = ARecord.Name);
end;
note The “hidden” self parameter is called this in C++ and Java, but it is called self in Objective-C
(and in Object Pascal, of course).