How to store an object into an integer?

Source: Internet
Author: User

How to store an object into an integer?

Sometimes you want to use your beloved, worldclass component, but it has no room for saving data anywhere. But to build a connection to that data you need is maybe to complex.

Here is an easy way to deal with it.

I had this project where I made a kind of Graphical Editor. I had to use components and objects that I cocould not alter. but I had to store my object-data somewhere into the interface, so I knew which object is selected and has to be dealt.

I found a simple solution as I browsed through possible places in the component.
It had a property called "tag", which almost every component has one.
But how to get my object into this property.

Well, I did it by deriving and classtyping.

Imagine a component like this one:

-----------------------------------------
Tsomecomponent = Class

Published
Property Tag: integer read ftag write ftag;
End;
-----------------------------------------

... And you have an object defined like this

-----------------------------------------
VaR
Myobject: tmyobjectclass;
-----------------------------------------

You can put your object into the tag like this:

-----------------------------------------
Somecomponent. Tag: = INTEGER (pointer (myobject ));
-----------------------------------------

Thats all.

If you have to lay hands on it again you have several possibilities.
You can simply typecast:

-----------------------------------------
Tmyobject (pointer (somecomponent. Tag). Name: = 'Peter ';
-----------------------------------------

... Or you cocould take a temporary variable inside a function for easier access:

-----------------------------------------
Procedure dosome (I: integer );
VaR
M: tmyobject;
Begin
M: = tmyobject (pointer (I ));
Showmessage (M. Name );
End;
-----------------------------------------

You cocould even save different objects into your components.
To proof if you have the right object you can help yourself
Using the tobject class.

-----------------------------------------
Function ismyobject (I: integer): Boolean;
Begin
Result: = tobject (pointer (I). classname = 'tmyobject ';
End;

Thats all I wanted to tell for now.

Have fun coding.
Nostromo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.