Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Interface / Pointer GUI Association

Posted on 2000-04-24
6
Medium Priority
?
242 Views
Last Modified: 2010-04-04
Hello All

I would like to know the best way to store an interface reference in an assocaited pointer.

For example, in a listbox containing cats, with each item representing an ICat reference, I would like to store the ICat reference in the Objects property of the list item. Another example is using a TTreeView (cats in a tree?) and the Node's Data property.

I have accomplished this in the past using records (one sole member of type ICat and using the record pointer), and also by bypassing reference counting by typecasting the interface to a void pointer. The first method is tedious, and the second one is dangerous.

Is there some elegant construct or alternate method out there that I do not know about? Sure, one can use TInterfaceList to maintain a list of references, but there is still no way of directly associating that reference with the Objects or Data properties, so this discussion is not further advanced by such an observation.

What do you do?

Regards,

Edo
0
Comment
Question by:Edo082297
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 2746436
You can do this:

var obj : TObject;
    cat : ICat;
begin
  ICat(obj) := cat;
  ICat(obj) := nil;

This will work perfectly, even with incrementing and decrementing the reference count. Of course you have to assign "nil" to all objects before freeing them, because otherwise the reference count is never decremented.

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2746437
The type of obj doesn't really matter, it just has to be a 4-byte type. You can also use integer/cardinal/pchar/pointer/whatEver instead of TObject.
0
 
LVL 1

Author Comment

by:Edo082297
ID: 2746722
Madshi,

Please re-read my question.

Wouldn't this be nice:

var
  ACat: ICat;
  ANode: TTreeNode;
begin
  ACat := CoCat.Create;
  ICat(ANode.Data) := ACat;
end;

Sorry, left side can't be assigned to.

Or:

AListBox.Items.AddObject(ACat.Name, IUnknown(ACat));

Of course, the compiler will swallow casting ACat to a void pointer, but that is dangerous!

We are back at square one.

Edo
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:Madshi
ID: 2746935
I thought this one would be possible:

var
  ACat: ICat;
  ANode: TTreeNode;
begin
  ACat := CoCat.Create;
  ICat(ANode.Data) := ACat;
end;

Okay, let's change it like this:

var
  ACat: ICat;
  APtr: pointer;
  ANode: TTreeNode;
begin
  ACat := CoCat.Create;
  ICat(APtr) := ACat;
  ICat(ANode.Data) := APtrt;
end;

To free it, do this:

  APtr := ANode.Data;
  ANode.Free;
  ICat(APtr) := nil;

Regards, Madshi.
0
 
LVL 1

Author Comment

by:Edo082297
ID: 2763960
ICat(ANode.Data) := APtr;

....left side cannot be assigned to

However, I can get the compiler to take the following:

ACat := CoCat.Create;

ICat(APtr) := ACat;
ANode.Data := APtr;

However, I'm not sure if ACat is reference counted properly in this last case; using the void pointer bothers me (should it?). I'll give you full points if you check that, or once I get around to checking it and see that it does indeed correctly increment the reference count on ACat (which could be in a couple of weeks at this point).

Thanks, Madshi

Edo
0
 
LVL 20

Accepted Solution

by:
Madshi earned 300 total points
ID: 2764061
Oooooops - ****!! Yes, you changed it exactly the way I meant it...   :-)

The reference count is incremented in the line "ICat(APtr) := ACat". So this solution has no flaws.

Regards, Madshi.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

705 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