What's the Delphi equivalent of 'this' in C++? Could you please give some examples of its use?
Asked
Active
Viewed 1,761 times
2 Answers
9
In delphi Self is the equivalent of this. It is also assignable as described in here.
-
3Note that, since it is passed by value, assigning to Self is not very useful, although it can be done. It will not, as some might expect, "return" nil from a constructor (constructors actually don't "return" anything - the assignment syntax is just a convenience). – Rudy Velthuis Jul 22 '11 at 22:46
-
1Hmmm...I see that the people in the SO question linked to by you say the same. – Rudy Velthuis Jul 22 '11 at 22:48
-
Assigning to Self is a corner case and normally you would not to need to do this as it might be considered as a bad practice. Yet, using Self as a reference to a class is useful only if one is using non-standard naming conventions. Standard conventions would be the use of F as a prefix of class variables (FIsCorrect as an example) and A as procedure/function arguments (AIsCorrect). Then, assignment of class variables would be as simple as FIsCorrect := AIsCorrect, alternatively you would need to use Self.IsCorrect := IsCorrect if function (procedure) argument is: IsCorrect: Boolean. – too Jul 22 '11 at 23:02
-
1Using with statement should be forbidden as it complicates things a lot more. – too Jul 22 '11 at 23:04
-
too: well yes, but how does that relate to Self? – Rudy Velthuis Jul 22 '11 at 23:13
-
@Rudy - One way is that if you're not careful inside a 'with' block, you could make a call on 'self' instead of on what you think is in your your 'with' block. Many moons ago it happened to me with 'free' nested in a 'with' block - every time I ran that function the app would disappear with an AV - traced it to unqualified call to free in the 'with' block that was resolving to 'self', which was the main form of the application... Since then, I have never used the 'with' construct LOL – Vector Jul 23 '11 at 18:17
-
@Mikey: OK, but somehow, what he wrote, doesn't make a lot of sense. Self is involved in many things, sure, but `with` is orthogonal to setting Self to nil. – Rudy Velthuis Jul 23 '11 at 20:14
-
@Rudy - agreed. It was a stretch on my part - just wanted to tell the story about 'with' mainly. :-) – Vector Jul 23 '11 at 22:20
-
"It is also assignable" - been using Delphi professionally nearly every day since it was released - I've never had the need, or even the thought, of doing such a thing. – Vector Jul 23 '11 at 22:25
-
@Mikey You're right. Assigning some value to `self` could be really confusing, and is not necessary, even to write faster generated code (e.g. assuming `self=eax` or `self=rcx` on a small method). – Arnaud Bouchez Jul 24 '11 at 14:47
4
In most cases, you should not use self
in the methods.
In fact, it's like if there was an implicit self.
prefix when you access the class properties and methods, within a class method:
type
TMyClass = class
public
Value: string;
procedure MyMethod;
procedure AddToList(List: TStrings);
end;
procedure TMyClass.MyMethod;
begin
Value := 'abc';
assert(self.Value='abc'); // same as assert(Value=10)
end;
The self
is to be used when you want to specify the current object to another method or object.
For instance:
procedure TMyClass.AddToList(List: TStrings);
var i: integer;
begin
List.AddObject(Value,self);
// check that the List[] only was populated via this method and this object
for i := 0 to List.Count-1 do
begin
assert(List[i]=Value);
assert(List.Objects[i]=self);
end;
end;
this above code will add an item to the TStrings
list, with List.Objects[] pointing to the TMyClass instance. And it will check this has been the case for all items of the List.

Arnaud Bouchez
- 42,305
- 3
- 71
- 159
-
@AB - "implicit self" - since I started messing with Python, which requires 'self' when referencing any internal class member, I've started doing that in Delphi as well - not sure if I like it or not stylistically, but it seems to facilitate quicker scanning of code. – Vector Jul 23 '11 at 22:29
-
@Mikey The common practice in Delphi is not to use `self.` prefix. In fact, the IDE intellisense allows you to quickly access the property names, or see the declaration with the mouse pop-up hint or Ctrl+Click - so there is no need to specify this prefix. If you don't define global variables in your code (this is required for good code), you know that the identifiers in your methods code are property/method names. Therefore, you should not use the `self.` prefix in your methods (unless inside a `with` statement). – Arnaud Bouchez Jul 24 '11 at 06:09
-
@AB - I know and agree with all you said but that doesnt negate my reason for using it. If you want to scan/speed read a piece of code, 'self' helps - often I like to sit back and just read code without any mouse or interaction at all, and I also still like to print out code and just read it. So it's the "self help" approach.... LOL – Vector Jul 24 '11 at 06:48
-
@Mikey I understand that it may "help your self" at first ;) But when you experience a bit about the implicit identifiers (like unit name, or self class instance) in Delphi, you'll find out it's some kind of elegant and with less typing. In Python, replacing begin/end with indentation help reduce typing (even if I usually don't write any `end` because my IDE does it for me, and the `begin` is faster to type on a French keyboard than a `{` key), but the `self.` made it back verbose in the python code. ;) – Arnaud Bouchez Jul 24 '11 at 14:43
-
@AB-I've been progamming constantly with Delphi since version 1.0 - have plenty of experience with implicit identifier style - that's why I said that stylistically I'm not so hot on qualifying 'self'. I never qualified self (or anything else in Delphi) except when absolutely necessary. But after using C# and Python I found qualified names can be helpful for the sake of clarity and safety. So I'm ambivilent about the question - much to be said for the brevity, efficiency and elegance of implicit resolution, but also much to be said for the clarity and margin of safety afforded by qualification. – Vector Jul 24 '11 at 17:56