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