Integer value Pointer

Posted on 2006-04-10
Last Modified: 2008-01-09
Ok this is interesting Question.

If you have noticed every component have property "TAG" and its integer. Not many components have property pointer. For exsample every TTreeNode has propery "Data" wich is poiner its value is nil and i can use this pointer for my extensions, like adding some class or so.

So what is my idea.

pointer value is integer only in hex decimal. So for better view i will show my idea

I creare TList.


I asign T pointer value to a pointer P


For my component i have "TAG" propery I want to add pointer valu to "TAG" converting pointer in integer value

it should look like so


i:=pointertoint(p);//i know pointertoint function doesn't exist :) but that is the idea i wan't to get pointer value to integer and beck from integer value to pointer :).


So why i'm so interested is this kind of taging :) if component doesn't have pointer poperty i can use "tag" property like pointer to some object.

But i need these to function:


I think this idea is nice i will continue to solve this interesting idea my self but fieel free to try to solve it first.
Question by:Kristao
    LVL 26

    Accepted Solution

    Because pointer and integer are both 4 byte data types, a simple cast can be used.

    var i: Integer;
         p: Pointer;

     i:=Integer(p); // Cast pointer to int
     p:=Pointer(i); // Cast int to pointer




    LVL 5

    Expert Comment

    Yep, Russel is spot on ...



    Stores an integer value as part of a component.

    property Tag: Longint;


    Tag has no predefined meaning. The Tag property is provided for the convenience of developers. It can be used for storing an additional integer value or it can be typecast to any 32-bit value such as a component reference or a pointer.

    LVL 3

    Expert Comment

    You can freely type cast any 32-bit variable (or less) to another 32-bit type.
    32 bit type in Delphi are: pointer, integer, cardinal, single.

    So if you want to 'save' a pointer value to the 'tag' property you can do that:

    Button1.Tag := Integer(Button2);

    which.. later on, you can do this:


    Or.... the other way around, you can create a list of integer values using TList:

    var I: Integer;
    MyList.Add(TObject(I)); // type cast integer to pointer type

    ... and retrieve it again:

    I := Integer(MyList.Items[0]);

    Note that the compiler will give you warning of unsafe typecasting, you'll need
    to be very carefull with this method.

    LVL 3

    Expert Comment

    Or... of course you can create your 2 conversions functions:

    function PointerToInt(APointer: Pointer): Integer;
      Result := Integer(APointer);

    function IntToPointer(AInt: Integer): Pointer;
      Result := Pointer(AInt);

    LVL 17

    Expert Comment

    here's a working example from 1 of my projects

    type TPositionObject = class
        startx, starty: integer;

    panel2.Tag := Integer(TPositionObject.Create);

    procedure TForm1.Panel_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
            TPositionObject( (Sender AS TPanel).Tag).startx := X;
            TPositionObject( (Sender AS TPanel).Tag).starty := Y;
            TPositionObject( (Sender AS TPanel).Tag).Moving := True;

    LVL 7

    Expert Comment

    Keep in mind that Pointer and Longint while in 32 bit windows are the same size.  That is not guaranteed in 64 bit windows.  Not sure how Delphi and Borland will/ or do handle this in 64 bit.
    LVL 5

    Expert Comment

    Also keep in mind that -some- exisiting 3rd party component sets use this for there own purposes, commonly, internationalisation and skinning components, for instance.
    LVL 1

    Expert Comment

    I created this little experiment to see what will happen.  I learned something today.

    procedure TForm1.Button1Click(Sender: TObject);
      _pointers : TList;
      x, iTestP, iTestV ,i: integer;
      lb, lsTest : TListBox;
      x := 5;
      lb := ListBox1; // A listbox on my form
      lb.Items.Add('Line 1');
      lb.Items.Add('Line 2');

      _pointers := TList.Create;
      //You need to add the @ before type variables to get a pointer to that value.
      _pointers.Add(lb);//Objects are always pointer.

      lsTest := TListBox(_pointers.Items[1]);//Retreive listbox pointer from IList
      iTestP := integer(_pointers.Items[0]);//This returns a pointer to integer value
      iTestV := integer(_pointers.Items[0]^);//Returns the value of the integer

      //iTestP is a pointer to my origional value.  You can also retreive that
      //value by
      iTestV := integer(pointer(iTestP)^);

      //This means you can store the pointer (iTestP) in the tag element, and
      //later on retreive the value like just above.

      ShowMessage('Pointer to origional value ' + IntToStr(iTestP));
      ShowMessage('Origional value ' + IntToStr(iTestV));

      for i := 0 to lsTest.Items.Count-1 do begin
    LVL 7

    Expert Comment

    ruanlab123 just for clarification in case someone reading your post doesn't understand it.

    you declared your x,iTestP,iTestv as Integers in the local context and got the address to those pointers with the @ operator.  These pointers are not valid outside this proc's scope.

    try this with your test.

    x :PInteger;  or X: ^Integer;  (same thing).

    now x is a true pointer type.

    New(X) will get the memory for an Integer and X will POINT to that memory (or in English the value of X is the @ of the memory returned from the New function).

    While the pointer X is local to the scope of your function.  The memory that X POINTS to is not.  So if you add x to a TList that is global to an object then it can be accessed by any code that has access to the object.  ex.  Tlist.Get accesses a list of pointers that are global to the TList object.

    When you wish to access the value that X points to you use the^.

        MyInt : Integer
       MyInt := X^;  not X;

    also You obtained the memory with the NEW function.  It is your responsibility to release it with the DISPOSE function.



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now