Solved

RTTI on classes ok.  On interfaces ok too?

Posted on 2000-04-05
12
354 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now