Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:02:37+00:00 2026-06-16T09:02:37+00:00

I’ve looked at many questions and resources which deal with the Self variable in

  • 0

I’ve looked at many questions and resources which deal with the “Self” variable in an Object, but everyone says something different.

For example, in this question: Delphi Self-Pointer usage

the highest rated answer to the question appears to be wrong. Pointer(Self) does not point to the object which contains it, and cannot be used to pass references from inside the object.

I’ve tried doing this:

Type
  myobject = class
    PSelf: Pointer;
  End;

Var
  Obj: myobject;

Procedure RunProgram;
Begin
  Obj := myobject.create;
  Obj.PSelf := @Obj;

  //Run the rest of the program
  .
  .
  .

and for the most part, this has worked just fine.

My question is: is this a good coding practice? Can the “PSelf” variable be expected to point to the object for the duration of the program’s execution?

I recently came across a bug where “PSelf” had stopped pointing to it’s containing object, and I’m wondering if objects ever get shuffled around in the heap, or whether the memory had been corrupted.

Edit:

There is some instance in which using the “Self” variable didn’t work for me, and now I cannot duplicate it. So this whole question is pointless, as is my technique of using a ‘PSelf’ variable. Sorry about that.

And as Ken pointed out, the link above has a correct answer 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:02:38+00:00Added an answer on June 16, 2026 at 9:02 am

    I think you’re misunderstanding what Self is, and how object references work in Delphi.

    A variable containing an instance of a class is already a pointer to that object instance. The Delphi compiler just allows you to leave out the dereference operator (^) as a convenience.

    var
      MyObject: TMyObject;  
    begin
      MyObject := TMyObject.Create;  // MyObject is now a pointer to an instance of TMyObject
      ...
    

    Delphi also allows the shorthand of not using the dereference operator when accessing members or properties of the object instance. Again, the following code is actually equivalent:

    MyObj.SomeProperty := SomeValue;
    MyObj^.SomeProperty := SomeValue;
    

    From the Delphi documentation:

    A variable of a class type is actually a pointer that references an object. Hence more than one variable can refer to the same object. Like other pointers, class-type variables can hold the value nil. But you don’t have to explicitly dereference a class-type variable to access the object it points to. For example, SomeObject.Size := 100 assigns the value 100 to the Size property of the object referenced by SomeObject; you would not write this as SomeObject^.Size := 100.

    Self is an automatically declared property that points to the current instance of the object. In other words, it’s automatically available inside the code that implements that class to reference the current instance of the object. This allows you to have multiple instances of the same object:

    type
      TMyObject=class(TObject)
      private
        FMyInteger: Integer;
        function GetMyInteger: Integer;
        procedure SetMyInteger(Value: Integer);
      published
        property MyInteger: Integer read GetMyInteger write SetMyInteger;
      end;
    
    ...
    function TMyObject.GetMyInteger: Integer;
    begin
      Result := Self.FMyInteger; 
    
      // Self is implied, so the above line can more simply be written as
      // Result := FMyInteger;
    end;
    
    procedure TMyObject.SetMyInteger(Value: Integer);
    begin
      if (Value <> Self.FMyInteger) then  // Self is again implied here
        Self.FMyInteger := Value;
    end;
    
    var
     MyObjOne, MyObjTwo: TMyObject;
     i, j: Integer;
    begin
      MyObjOne := TMyObject;
      // Here, the code inside TMyObject.SetInteger that
      // uses `Self` would refer to `MyObjOne`
      MyObjOne.MyInteger := 1; 
    
      MyObjTwo := TMyObject;
      // Here, the code in TMyObject.SetInteger would be
      // using the memory in `MyObjTwo` when using `Self`
      MyObjTwo.MyInteger := 2; 
    end;        
    

    Note that Self is only valid in the code that implements the class. It’s available and valid in TMyObject.GetMyInteger and TMyObject.SetMyInteger above (the only implemented code in my example), and always refers to the current instance.

    There’s no need to keep track of the addresses of Self, as the variable referencing that object instance is Self inside methods of that object instance. It’s only valid inside that instance of the object, and always refers to that object instance. So in your code example, PSelf is just wasted space – myobject already contains a pointer to itself, and that pointer is automatically available in methods of myobject:

    type
      myobject = class;   // Now automatically contains a `Self` pointer
                          // available in methods of the class once an
                          // instance is created
    
    var
      myobj: myobject;
    begin
      myobj := myobject.Create;  // myobj.Self now is valid in methods of
                                 // `myobject`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.