Board index » delphi » Tag property and Pointers

Tag property and Pointers

I'd like to use the tag property of a component (specifically a TabSheet) to
store a pointer to an object I've created, which is a descendent of
TComponent. I'm doing something like this to make the assignment in
FormShow:

                                        TabSheet1.Tag := LongInt(@MyObject);

where TabSheet1 is on PageControl1 on my form, and MyObject is of type
TMyClass = class(TComponent), also on the form.

Later I want to pass the object that is pointed to by the tag of the active
tab sheet into a funciton that takes a TMyClass for a parameter:

MyFunction(PageControl1.ActivePage.Tag);

However, this gives me and access violation. So does this:

MyFunction(Ptr(PageControl1.ActivePage.Tag));
and

MyFunction(TMyClass(PageControl1.ActivePage.Tag));

What am I doing wrong?

Justin Mathews
ADM Systems Group Inc.

 

Re:Tag property and Pointers


Quote
Gordon Morison wrote in message <7a207g$p0...@forums.borland.com>...
>I'd like to use the tag property of a component (specifically a TabSheet)
to
>store a pointer to an object I've created, which is a descendent of
>TComponent. I'm doing something like this to make the assignment in
>FormShow:

>                                        TabSheet1.Tag :=

LongInt(@MyObject);

You do not need the @ operator here. All variables of class types are
already pointers, so simply cast it directly:

  TabSheet1.Tag := LongInt(MyObject);

Then get it back with MyFunction( TMyObject(TabSheet1.Tag) ).

--
Wayne Niddery - WinWright Consulting
Delphi, C++Builder, JBuilder, InterDev --
http://home.ican.net/~wniddery/RADBooks.html
...remove chaff when replying...
"You know you've landed gear-up when it takes full power to taxi"

Other Threads