Solved

RTTI on classes ok.  On interfaces ok too?

Posted on 2000-04-05
12
370 Views
Last Modified: 2010-04-04
I'm getting a compiler error.  
Operator not applicable to this operand type

I just want to see if the Sender is a particular class.  RTTI.   I've done similar things like this before in other projects, but this will not compile:

procedure TfrmMain.editorStartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  if (Sender is TSNode) then
    doit( Sender as TSNode );
end;

Why?   TSNode is defined.  But its an interface...

  TSNode = interface(IDispatch)
    ['{717C48CA-1951-11D1-8150-0000C09865B6}']


0
Comment
Question by:abulka
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 2688813
Nope, that doesn't work. Objects and interfaces are a quite different thing.

How is this editorStartDrag called? How could that happen that Sender is an interface? Do you call editorStartDrag with an interface as a parameter?

Regards, Madshi.
0
 

Author Comment

by:abulka
ID: 2689195
The component generates the editorStartDrag() event, though I now doubt it ever sends an interface as a parameter.

So perhaps this question is best anwered in general terms e.g.

I can see if an object is a particular class using 'is'.

I can see if an object supports a particular interface using QueryInterface - but that assumes that the object is derived from InterfacedObject.

I can get to an interface via an object.
Can I get to an object via an interface?

Are these the relevant permutations/combinations - have I missed any useful ones?  This would answer my question.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2689248
Yes, now you're sounding like you understood the stuff...   :-))

>> Can I get to an object via an interface?

Normally you simply cannot, except if the interface implements a method like "GetObject".

Please look here:

http://community.borland.com/

There you'll find two good articles about interfaces&objects. They might help you to dig even deeper into the material.

Regards, Madshi.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:abulka
ID: 2689360
Adjusted points from 20 to 30
0
 

Author Comment

by:abulka
ID: 2689361
Alternatively to answering the above general questions, here is a revised question based on a REAL problem, which is based on exactly the same issue:

Given
  TSDListIteratorDisp = dispinterface
    ['{717C48CE-1951-11D1-8150-0000C09865B6}']
    procedure moveToStart; dispid 1610743808;
    procedure moveToEnd; dispid 1610743809;
    procedure next; dispid 1610743810;
    procedure previous; dispid 1610743811;
    procedure insertBefore(object_: IDispatch); dispid 1610743812;
    procedure insertAfter(object_: IDispatch); dispid 1610743813;
    function isWithinList: WordBool; dispid 1610743814;
    function getObject: IDispatch; dispid 1610743815;
    function clone: TSDListIterator; dispid 1610743816;
    property locationInDList: Integer readonly dispid 1610743817;
    procedure moveDListAfter(const moveDList: TSDList); dispid 1610743818;
    function moveToObject(object_: IDispatch): WordBool; dispid 1610743819;
    procedure removeCurrentObject; dispid 1610743820;
  end;

....

var
  childrenNodeDList, childrenEdgeList : TSDList;
  iter : TSDListIterator;
  anode : TSNode;
begin
    somenode.findChildren(childrenNodeDList, childrenEdgeList, TS_FIND_DEPTH_ALL);
    iter := childrenNodeDList.newIterator;

    while iter.isWithinList do begin
      if iter.getObject is TSNode then  // FAILS to compile
        anode := TSNode( iter.getObject );
      frmMain.editor.selectNode( anode, false );
      iter.next;
    end;

the problem is that iter.getObject returns something, and I want to make sure it is a TSNode interface so that I can pass it to the selectNode() method.

At the moment the .selectNode method is complaining at runtime that the node I am passing is not really a node.

I'm pretty sure that each .getObject is giving me a node, but perhaps its not quite the right aspect/interface that .selectNode  expects?

This might help:
  TSNode = interface(IDispatch)
    ['{717C48CA-1951-11D1-8150-0000C09865B6}']
    function Get_allowOcclusion: WordBool; safecall;
    procedure Set_allowOcclusion(Value: WordBool); safecall;
    function Get_bottom: Integer; safecall;
    procedure buildExpandedDigraphDList(const expandedDigraphDList: TSDList); safecall;
    function collapse: TSExpandReturnCode; safecall;
    function expand: TSExpandReturnCode; safecall;
    procedure findChildren(const newNodeDList, newEdgeDList: TSDList; depth: Integer); safecall;
....
....

0
 
LVL 20

Expert Comment

by:Madshi
ID: 2691070
This can't work, since GetObject gives you an IDispatch interface, while the selectNode function (or the code "getObject is TSNode") wants to have an object reference. That are simply two different things that don't fit together! You will NEVER get a valid TSNode from getObject, since getObject returns a completely different kind of thing, namely an interface instead of an object.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 30 total points
ID: 2691097
Oh, wait a moment, now you've confused me!!   :-)
You should name interfaces with a "I" as the first letter, so "TSNode" should be "ISNode". I thought it would be an object because of the name...
Well, what type does selectNode wants to have as the first parameter?

You can't do "if interface is IOtherInterface". But you can do this:

var iu1 : IUnknown;
begin
  blabla.getObject.QueryInterface(TSNode, iu1) = 0 then
    // blabla does support TSNode
    selectNode(blabla as TSNode, ...);

Now I guessed that selectNode looks like this:

procedure selectNode(blabla: TSNode; ...);

Right?
0
 

Author Comment

by:abulka
ID: 2808129
> Now I guessed that selectNode
> looks like this:
>
> procedure selectNode(blabla: TSNode; ...);
>
> Right?

Right!  

And the TSNode SHOULD be called ITSnode or something - but the activeX component supplier chose the names :-(  - they are C++ people!

P.S. Sorry for the delay in answering.
0
 

Expert Comment

by:topkapi
ID: 3329974
im sorry Madshi, but you are wrong.

you *can* use the typecast

  (Object as Interface)

as long as the object explicitly supports the IUnknown interface

  TMyObject = class(TInterfacedObject, IUnknown, ISNode);

Delphi (read: the IS and AS operators) use the QueryInterface function of IUnknown to find out wether or not the object supports the interface asked. unfortunatly delphi does 'forget' that any interface descends from IUnknown.

Regards,

-= topkapi =-
0
 

Expert Comment

by:topkapi
ID: 3330451
correction: you can not use IS, but you can use AS with interfaces

a code example:

  IWhatever = interface
    ['{753CC699-582A-11D4-BD41-5254AB1564F8}']
    procedure A;
  end;

  TMyObject = class(TInterfacedObject, IUnknown, IWhatever)

  protected
    procedure A;
  end;


procedure TForm1.Button1Click(Sender: TObject);
var
  obj : TMyObject;
  i, j: IWhatever;
begin
  obj := TMyObject.Create;
  // note: IS can not be used, but QueryInterface can do the same
  obj.QueryInterface(IWhatever, i);
  if assigned(i) then begin
    j := obj as IWhatever;
    j.A;
    // note: object used as interface, so it is cleaned up for us
  end
  else begin
    obj.Free;
  end;
end;

i hope this helps,

-= topkapi =-
0
 
LVL 20

Expert Comment

by:Madshi
ID: 3331290
>> im sorry Madshi, but you are wrong.

No, I'm not.

>> you *can* use the typecast
>>  (Object as Interface)
>> as long as the object explicitly supports the IUnknown interface

Of course, I never said anything different from that.

Regards, Madshi.
0
 

Expert Comment

by:topkapi
ID: 3331815
sorry, i read to fast. you were right ;-)

-= topkapi =-
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, left to enroll

632 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