Take a look at here:
http://stackoverflow.com/questions/4138211/how-to-cast-a-interface-to-a-object-in-delphi
Does Lazarus support TObject(IInterface) ? or I should make GetObject function like the above answer?
Regards, Ara

Mailing lists / lazarus / 2015-03
48 messages · 9 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Take a look at here:
http://stackoverflow.com/questions/4138211/how-to-cast-a-interface-to-a-object-in-delphi
Does Lazarus support TObject(IInterface) ? or I should make GetObject function like the above answer?
Regards, Ara
aradeonas wroteDoes Lazarus support TObject(IInterface) ? or I should make GetObject function like the above answer?
Just curious as to why you would need or want to do this? After all, how did you get the interface reference in the first place... Some object had to be queried to get it.
Regards,
- Graeme -
For example I have interface for filetypes that have type,size,name but have class for video,audio and picture and I want to add them in the list and then get them and get name and size and other propertice considering type.
Regards,
Ara
So work using objects directly.
Why do you want to use interfaces if you will use casting for a concrete class?
Marcos Douglas
Here an example for you:
typeWhat you think?
Ara
You should make IVideofile with GetIWidth and so on.
Here you should make IAudioFile with GetArtist and so on.
If you use interfaces all the way as suggested above, you can get one interface, and then query for the others as well with the IUnknown interface. then there is no need to access the objects.
Your problem is that you mix an interface with a non-interface approach.
Michael.
Here an example for you: type What you think? Ara http://www.fastmail.com - A no…
For do that, you do not need interface... but if you still continues using IFile for others reasons, the better use when we work using interfaces, is using variables that have the type of these interfaces.
Regards,
Marcos Douglas
You should make IVideofile with GetIWidth and so on. Here you should make IAudioFile with…
Michael Van Canneyt wroteYour problem is that you mix an interface with a non-interface approach.
+1
And my thought from the beginning.
Regards,
- Graeme -
I can get what you mean,Can you explain more?
For example when I want to store video and audio in one list how can I do that?
Regards,
Ara
I'm still don't get how cast interface in Lazarus.
Regards,
Ara
Please avoid top-posting unless you have a grave reason for it (like screen reader requirement).
I think (I'm a noob here too)
what Marcos, Michael and Graeme want to say, is
- first declare Interfaces, inheriting from those you want,
(namely IVideoFile, IAudioFile with appropriate get/set methods)
- then make IFile inherit from the above and from IUnknown, not the other way round
(hope I got that right /) )
el es
So then how can I add them in a list?In the end I want add video and audio files in one list together.
Regards,
Ara
Please avoid top-posting unless you have a grave reason for it (like screen reader…
Lukasz Sokol wrotePlease avoid top-posting unless you have a grave reason for it (like screen reader requirement).
And the old one that NOBODY adheres too.... Please trim unnecessary quotes - it takes all of ONE SECOND. It's wasting valuable time for the reader having to scroll pages and pages just to find a one line reply.
Regards,
- Graeme -
So then how can I add them in a list?In the end I want add video and audio files in one…
FYI: Your previous message was 72 lines of quotes and 2 lines of new content. That is ? 97% waisted space.
Regards,
- Graeme -
Its my mail service,I will delete replies but still my problem doesn't solve.
Thank you for your helps,
Ara
aradeonas wrotestill my problem doesn't solve.
Do you really need multiple inheritance (this seems to be the cause why you decided to go with Interfaces instead of pure classes) ?
This adds some complexity that in most cases can be avoided.
-Michael
Yes I need and I just need casting.
Ara
aradeonas wroteYes I need
In fact I doubt this.
(And don't do top-posing.This is very impolite.)
-Michael
Excuse me Michael,
I didn't know what top-posting mean until now[1].(Why is it impolite,I ask because really I don't know and thank you for mention it). About question: What you suggest for this need: We have types of file that have some shared properties like name and size but have more like Music artist or video width and we want to add them in a list like playlist. What is the best way to do this?
Regards, Ara
Links:
1. http://en.wikipedia.org/wiki/Posting_style#Top-posting
aradeonas wroteExcuse me Michael, ? I didn't know what top-posting mean until now.(Why is it impolite,I ask because really I don't know and thank you for mention it).
Forums are used for conversations.
People sometimes must jump in the conversation without reading the whole thread. Sometimes multiple questions are asked in a single post.
Top posting destroys the logic of the conversation: It requires you to jump forwards and backwards in the mail. This is especially so if there are multiple questions in the mail.
With inline replies, you can read through the mail and see the questions and their answer logically grouped together.
About question: What you suggest for this need: We have types of file that have some shared properties like name and size but have more like Music artist or video width and we want to add them in a list like playlist. What is the best way to do this?
It seems like a simple case, so:
Simply define 1 class that has all properties you are interested in, and a 'FileType : TMyFileType' property where TMyFileType = (ftNormal,ftAudio,ftVideo)
and define a TObjectList descendent (or use a generic list or collection) using this object.
I don't see the need to use interfaces in this case, unless you need additional features which you did not describe.
Michael.
Thank you for explaining,My email serves close all quotes and for this I didn't understand this problem until you said.Thank you.
About question you mean make a File class with name,size,artist,width and all others in one place?If this I didn't like it,It seem messy,I want to get file,cast it to video (if it is TVideoFile) and then work with it,It also prevent mistakes.
Regards, Ara
aradeonas wroteThank you for explaining,My email serves close all quotes and for this I didn't understand this problem until you said.Thank you. ? About question you mean make a File class with name,size,artist,width and all others in one place?If this I didn't like it,It seem messy,I want to get file,cast it to video (if it is TVideoFile) and then work with it,It also prevent mistakes.
That is as you prefer...
My motto is KISS.
Michael.
OK,So in the end how can I cast a Interface to a Object in Lazarus?
Regards,
Ara
That is as you prefer... My motto is KISS. Michael.
OK,So in the end how can I cast a Interface to a Object in Lazarus?
Regards,
Ara
And the old one that NOBODY adheres too.... Please trim unnecessary quotes - it takes all…
OOPS :) Apologies for that...
BTW The OP's gone a bit overboard now, trimming all the quotes ...
(on my prev. reply I just wanted to include some context... forgot to trim sigs.)
el es
OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wroteOK,So in the end how can I cast a Interface to a Object in Lazarus?
You can not. You are not even supposed to be able to do that.
You can only query other interfaces from an existing interface.
See my first mail: define N interfaces. When you receive an IFile interface from somewhere, query it for the other interfaces:
Var
MyFile : IFile;
MyVideo : IVideo;
begin
MyFile:=MyFileList[i];
if MyFile.QueryInterface(IVideo,MyVideo)=0 then
begin
// MyVideo now holds a IVideo interface.
end;
end;Michael.
OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wroteOK,So in the end how can I cast a Interface to a Object in Lazarus?
I usually add a "function getinstance(): <theBaseImplemetationClass>" to the interface definition.
BTW, your patience is admirable. :-)
Martin
Which goes completely against the very purpose of interfaces.
If you need this, you should not be using interfaces to begin with.
The only possible excuse is an external requirement to use interfaces.
Michael.
Thank you for explaining,My email serves close all quotes and for this I didn't…
aradeonas wroteAbout question you mean make a File class with name,size,artist,width and all others in one place?If this I didn't like it,It seem messy,
I agree, I wouldn't do it that way either. I would have subclasses which gives more specific details of each media type.
TBaseFile = class(TObject)
published
property Name: string...
property Size: integer...
property Caption: string...
end;
TVideoFile = class(TBaseFile)
published
property Width: integer...
end;
TAudioFile = class(TBaseFile)
published
property Artist: string...
end;
In your listview somewhere
ListView.Items.AddObject(obj.Caption, obj);obj can be a TBaseFile, TVideoFile or TAudioFile instance.
Then in your listview's OnPaint event (on whatever event draws each row)
var
obj: TBaseFile;
begin
obj := ListView.Items[Listview.Selected] as TBaseFile;
if obj is TAudioFile then
begin
// do something with TAudioFile(obj).Artist etc.
end
else if obj is TVideoFile then
begin
// do something with TVideoFile(obj).Width etc
end
else if obj is TBaseFile then
begin
// do whatever
end;
end;This is just one possible way of doing it. Interface can obviously also be used in a similar way, where one interface descends from another.
Regards,
- Graeme -
I usually add a "function getinstance(): <theBaseImplemetationClass>" to the interface…
Martin Schreiber wroteBTW, your patience is admirable. :-)
:-D Very true!
Regards,
- Graeme -
Which goes completely against the very purpose of interfaces. If you need this, you…
Michael Van Canneyt wroteWhich goes completely against the very purpose of interfaces.
+1
I've used interface for many projects over the years, and never needed to do that.
Regards,
- Graeme -
Which goes completely against the very purpose of interfaces. If you need this, you…
I do not agree.
Martin
OOPS :) Apologies for that... BTW The OP's gone a bit overboard now, trimming all the…
Lukasz Sokol wroteOOPS :) Apologies for that...
No worries. Most email clients allow you to select some text, then click the Reply button, automatically trimming all non-selected text for the reply email. Hence I say it takes one second to trim replies.
If your email client doesn't support that, get a more feature complete email client.
Descent email clients (NOT Gmail) also keep conversations threaded, which means even with very little quotes, it is easy to see what the previous message was and re-read it if really needed. And yes you can use a decent email client to read your Gmail via IMAP4.
Regards,
- Graeme -
I do not agree. Martin
No surprise there, I assume you would not have done it otherwise. It is a free world, after all.
I consider that very wrong design, and this is the reason the OP had to wait for his answer so long; I tried to provide alternatives for what is in my personal opinion a unnecessarily complicated and convoluted design.
The fact that no-one else bothered to (or could) reply is IMHO enough of a hint that the design is maybe not optimal ?
Michael.
No worries. Most email clients allow you to select some text, then click the Reply…
@Martin and @Graeme if it is a sarcasm it is a good one.(Im not good at finding out sarcasm). @Lukasz Im agree with Graeme,The best mail service I ever seen is FastMail (what im using) but its only commercial and I have this account from OperaMail time (Really good browser that I still use its benefits!) @Graeme your way is probably what I need.Thank you. @Michael I respect what you think and say,after all because of developers like you we are using Lazarus and FreePascal,I asking this question with reference to this question[1] and its the reason I asked this question.
Regards, Ara
Links:
1. http://stackoverflow.com/questions/4138211/how-to-cast-a-interface-to-a-object-in-delphi
aradeonas wrote@Martin and @Graeme if it is a sarcasm it is a good one.(Im not good at finding out sarcasm). @Lukasz Im agree with Graeme,The best mail service I ever seen is FastMail (what im using) but its only commercial and I have this account from OperaMail time (Really good browser that I still use its benefits!) @Graeme your way is probably what I need.Thank you.
Graeme's way would have been my choice too, depending on what you want to do.
@Michael I respect what you think and say,after all because of developers like you we are using Lazarus and FreePascal,I asking this question with reference to this question and its the reason I asked this question.
No problem.
Feel free to ask questions, that is why we have a mailing list.
Michael.
OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wroteSo in the end how can I cast a Interface to a Object in Lazarus?
You can't. An interface "contains" one or more object(s), so you can't "cast" it to same.
-Michael
@Martin and @Graeme if it is a sarcasm it is a good one.(Im not good at finding out…
aradeonas wrote@Martin and @Graeme if it is a sarcasm it is a good one.(Im not good at finding out sarcasm).
No, it is no sarcasm from my side. In Free Pascal community sometimes one needs much patience if one does not comply with the main stream and I think you made it very good.
Martin
Which goes completely against the very purpose of interfaces. If you need this, you…
+1
Marcos Douglas
OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wroteOK,So in the end how can I cast a Interface to a Object in Lazarus?
The syntax show here:
http://docwiki.embarcadero.com/RADStudio/XE7/en/Interface_References#Casting_Interface_References_to_Objects works in FPC as well.
var
LIntfRef: IInterface;
LObj: TInterfacedObject;
begin
{ Create an interfaced object and extract an interface from it. }
LIntfRef := TInterfacedObject.Create();
{ Cast the interface back to the original object. }
LObj := LIntfRef as TInterfacedObject;
endBUT, it would only work, if you're using COM interfaces (wont work corba). It works for COM interfaces only because FPC just checks that an interface implements IObjectInstance (via COM queryinterface method).
IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';All pascal objects implement it, thus you don't have to specify it implicitly.
If you're not using COM interfaces, then you'll need to add getObject() method to your interface.
thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/8b60a912/attachment.html>
Dmitry Boyarintsev wroteBUT, it would only work, if you're using COM interfaces (wont work corba).
Sorry, but that is a false statement. It works for both COM and CORBA interfaces. Simply don't use the 'as' syntax - just cast it.
Attached is an example project showing both COM and CORBA works fine. Simply toggle the DEFINE at the top of the code.
Regards,
- Graeme -
On Fri, Mar 13, 2015 at 10:21 AM, Graeme Geldenhuys <
Wait, how can a syntax "work", if not used? :)
Don't forget that Pascal is safe-type language.
So "as" has also intent of checking an interface for being castable to an object instance.
If an interface is originating from non-FPC environment (i.e. delphi, c++ or whatever langauge) such casting should fail.
This is not the case for OP, but should be used as good practices.
Please note that casting Interface to TObject via "as" was not originally in the compiler. It was eventually added for Delphi compatibility. Previously compiler would just fail with an error that interface is not castable to object at all (I.e. Delphi 7 won't allow the syntax as well)
http://www.freepascal.org/docs-html/ref/refsu47.html#x141-15100012.8.7
thanks,
Dmitry
P.S. By the way, if different versions of FPC are used (i.e. an interface is loaded from a DLL) and then interface is typecasted to an object via "as". It might cause problems if TObject structures are different between host application and the library. But using interfaces only prevents the problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/ef29a051/attachment.html>
I'm not sure if this point has been made already, but interface cast (or querying as defined by COM), only works if the interface has a GUID assigned. When this is the case, in delphi and I believe in {$mode delphi} you can cast between object and interfaces like so:
procedure Test(Sender: TObject);
var
A: IMyInterfaceType;
B: TMyObjectType;
begin
if Sender is IMyInterfaceType then
A := Sender as IMyInterfaceType;
if A is TMyObjectType then
B := A as TMyObjectType;
// other code here
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/677953c4/attachment.html>On Fri, Mar 13, 2015 at 10:21 AM, Graeme Geldenhuys < Wait, how can a syntax "work", if…
Dmitry Boyarintsev wroteDon't forget that Pascal is safe-type language. So "as" has also intent of checking an interface for being castable to an object instance.
As far as I know, that is just one of the differences between COM and CORBA interfaces. I don't presume to know the internals but believe the implementation differences (between 'as' and a direct typecast) is something to do with GUID's and IUnknown availability. If they behaved exactly the same in every instance, then FPC wouldn't have had two interface modes. ;-)
Hence decide which interface mode suites you best.
If an interface is originating from non-FPC environment
That is beyond the scope of my previous answers. I'm only referring to FPC developed applications.
Regards,
- Graeme -
Thanks Martin. Graeme I tested your way but you cant get property values with this way,if you test code that I sent before showmessage will show nothing. but writeln will work.
Regards, Ara
aradeonas wroteGraeme I tested your way but you cant get property values with this way
See my slightly modified test.pas program from earlier. It seems that using COM and 'as' is the preferred way when you want an object instance from an interface reference. I never noticed the compiler Warning in my previous test.pas program.
attached is a new test.pas and it clearly shows that you CAN call a method and reference a property without problems.... as long as you use COM interfaces.
Here is the new program output:
[tmp]$ ./test
via interface: My Caption
Pass: we got the object
Hello from the object
via object instance: My Caption
Regards,
- Graeme -
Interesting,Thank you.
I'm not familiar with COM interfaces.
Just using {$interfaces com} do the job?
Regards,
Ara
aradeonas wroteJust using {$interfaces com} do the job?
Yes, and COM interfaces are reference counted by default.
Regards,
- Graeme -