Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-03

How to cast a Interface to a Object in Lazaeus

48 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

aradeonas

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

http://www.fastmail.com - Access your email from home and the web -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150312/2abf36e0/attachment.html>

Reply by email

Graeme Geldenhuys
to aradeonas
aradeonas wrote
Does 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

aradeonas
to Graeme Geldenhuys

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

Graeme Geldenhuys wrote 20 lines
On 2015-03-12 11:12, aradeonas wrote: > Does 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 - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - Accessible with your email software or over the web

Reply by email

Marcos Douglas
to aradeonas
aradeonas wrote 7 lines
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

Reply by email

aradeonas
to Marcos Douglas

Here an example for you:

type
Show quoted text 53 lines
{ IFile } IFile = interface function GetName: string; procedure SetName(AValue: string); property Name: string read GetName write SetName; end; TFileList=specialize TFPGList<IFile>; { TVideoFile } TVideoFile = class(TInterfacedPersistent, IFile) private FName: string; FWidth: integer; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Width: integer read FWidth write FWidth; end; { TAudioFile } TAudioFile = class(TInterfacedPersistent, IFile) private FName: string; FArtist: string; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Artist: string read FArtist write FArtist; end; var Form1: TForm1; implementation {$R *.lfm} { TAudioFile } function TAudioFile.GetName: string; begin Result:=FName; end; procedure TAudioFile.SetName(AValue: string); begin FName:=AValue; end; { TVideoFile } function TVideoFile.GetName: string; begin Result:=FName; end; procedure TVideoFile.SetName(AValue: string); begin FName:=AValue; end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject); var V1,V2:TVideoFile; A1,A2:TAudioFile; Fl:TFileList; begin Fl:=TFileList.Create; V1:=TVideoFile.Create; V1.Name:='V'; V1.Width:=100; A1:=TAudioFile.Create; A1.Name:='A'; A1.Artist:='Art'; FL.Add(V1); FL.Add(A1); V2:=TVideoFile(FL.Items[0]); A2:=TAudioFile(Fl.Items[1]); ShowMessage(V2.Name); ShowMessage(A2.Name); end;

What you think?

Ara

Marcos Douglas wrote 17 lines
On Thu, Mar 12, 2015 at 8:39 AM, aradeonas wrote: > > 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 -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - A no graphics, no pop-ups email service -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150312/315fdaa1/attachment.html>

Reply by email

Michael Van Canneyt
to aradeonas
aradeonas wrote 27 lines
Here an example for you: ? type ? { IFile } ? IFile = interface function GetName: string; procedure SetName(AValue: string); property Name: string read GetName write SetName; end; ? TFileList=specialize TFPGList<IFile>; ? ? { TVideoFile } ? TVideoFile = class(TInterfacedPersistent, IFile) private FName: string; FWidth: integer; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Width: integer read FWidth write FWidth; end;

You should make IVideofile with GetIWidth and so on.

Show quoted text 12 lines
{ TAudioFile } ? TAudioFile = class(TInterfacedPersistent, IFile) private FName: string; FArtist: string; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Artist: string read FArtist write FArtist; end;

Here you should make IAudioFile with GetArtist and so on.

Show quoted text 58 lines
? var Form1: TForm1; ? implementation ? {$R *.lfm} ? { TAudioFile } ? function TAudioFile.GetName: string; begin Result:=FName; end; ? procedure TAudioFile.SetName(AValue: string); begin FName:=AValue; end; ? { TVideoFile } ? function TVideoFile.GetName: string; begin Result:=FName; end; ? procedure TVideoFile.SetName(AValue: string); begin FName:=AValue; end; ? ? { TForm1 } ? procedure TForm1.FormCreate(Sender: TObject); var V1,V2:TVideoFile; A1,A2:TAudioFile; Fl:TFileList; begin Fl:=TFileList.Create; V1:=TVideoFile.Create; V1.Name:='V'; V1.Width:=100; A1:=TAudioFile.Create; A1.Name:='A'; A1.Artist:='Art'; FL.Add(V1); FL.Add(A1); V2:=TVideoFile(FL.Items[0]); A2:=TAudioFile(Fl.Items[1]); ShowMessage(V2.Name); ShowMessage(A2.Name); end; ? What you think?

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.

Reply by email

Marcos Douglas
to aradeonas: Here an example for you: type What you think? Ara http://www.fastmail.com - A no…
aradeonas wrote 98 lines
Here an example for you: type { IFile } IFile = interface function GetName: string; procedure SetName(AValue: string); property Name: string read GetName write SetName; end; TFileList=specialize TFPGList<IFile>; { TVideoFile } TVideoFile = class(TInterfacedPersistent, IFile) private FName: string; FWidth: integer; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Width: integer read FWidth write FWidth; end; { TAudioFile } TAudioFile = class(TInterfacedPersistent, IFile) private FName: string; FArtist: string; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Artist: string read FArtist write FArtist; end; var Form1: TForm1; implementation {$R *.lfm} { TAudioFile } function TAudioFile.GetName: string; begin Result:=FName; end; procedure TAudioFile.SetName(AValue: string); begin FName:=AValue; end; { TVideoFile } function TVideoFile.GetName: string; begin Result:=FName; end; procedure TVideoFile.SetName(AValue: string); begin FName:=AValue; end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject); var V1,V2:TVideoFile; A1,A2:TAudioFile; Fl:TFileList; begin Fl:=TFileList.Create; V1:=TVideoFile.Create; V1.Name:='V'; V1.Width:=100; A1:=TAudioFile.Create; A1.Name:='A'; A1.Artist:='Art'; FL.Add(V1); FL.Add(A1); V2:=TVideoFile(FL.Items[0]); A2:=TAudioFile(Fl.Items[1]); ShowMessage(V2.Name); ShowMessage(A2.Name); end; What you think?

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

Reply by email

aradeonas
to Graeme Geldenhuys

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

Graeme Geldenhuys wrote 18 lines
On 2015-03-12 12:35, Michael Van Canneyt wrote: > Your problem is that you mix an interface with a non-interface approach. +1 And my thought from the beginning. Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - A no graphics, no pop-ups email service

Reply by email

aradeonas
to aradeonas

I'm still don't get how cast interface in Lazarus.

Regards,
Ara

aradeonas wrote 36 lines
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 On Thu, Mar 12, 2015, at 05:42 AM, Graeme Geldenhuys wrote: > On 2015-03-12 12:35, Michael Van Canneyt wrote: > > Your problem is that you mix an interface with a non-interface approach. > > +1 > And my thought from the beginning. > > Regards, > - Graeme - > > -- > fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal > http://fpgui.sourceforge.net/ > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- http://www.fastmail.com - A no graphics, no pop-ups email service -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - The way an email service should be

Reply by email

Lukasz Sokol
to aradeonas

Please avoid top-posting unless you have a grave reason for it (like screen reader requirement).

aradeonas wrote 5 lines
I'm still don't get how cast interface in Lazarus. Regards, Ara

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

Show quoted text 38 lines
On Thu, Mar 12, 2015, at 05:56 AM, aradeonas wrote: > 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 > > On Thu, Mar 12, 2015, at 05:42 AM, Graeme Geldenhuys wrote: >> On 2015-03-12 12:35, Michael Van Canneyt wrote: >>> Your problem is that you mix an interface with a non-interface approach. >> >> +1 >> And my thought from the beginning. >> >> Regards, >> - Graeme - >> >> -- >> fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal >> http://fpgui.sourceforge.net/ >> >> -- >> _______________________________________________ >> Lazarus mailing list >> >> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > > -- > http://www.fastmail.com - A no graphics, no pop-ups email service > > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

aradeonas
to Lukasz Sokol

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

Lukasz Sokol wrote 70 lines
Please avoid top-posting unless you have a grave reason for it (like screen reader requirement). On 12/03/15 15:56, aradeonas wrote: > I'm still don't get how cast interface in Lazarus. > > Regards, > Ara > 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 > On Thu, Mar 12, 2015, at 05:56 AM, aradeonas wrote: >> 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 >> >> On Thu, Mar 12, 2015, at 05:42 AM, Graeme Geldenhuys wrote: >>> On 2015-03-12 12:35, Michael Van Canneyt wrote: >>>> Your problem is that you mix an interface with a non-interface approach. >>> >>> +1 >>> And my thought from the beginning. >>> >>> Regards, >>> - Graeme - >>> >>> -- >>> fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal >>> http://fpgui.sourceforge.net/ >>> >>> -- >>> _______________________________________________ >>> Lazarus mailing list >>> >>> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus >> >> -- >> http://www.fastmail.com - A no graphics, no pop-ups email service >> >> >> -- >> _______________________________________________ >> Lazarus mailing list >> >> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - Accessible with your email software or over the web

Reply by email

Graeme Geldenhuys
to Lukasz Sokol: Please avoid top-posting unless you have a grave reason for it (like screen reader…
Lukasz Sokol wrote
Please 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Michael Schnell
to aradeonas
aradeonas wrote
still 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

Reply by email

aradeonas
to Michael Schnell

Yes I need and I just need casting.

Ara

Michael Schnell wrote 15 lines
On 03/12/2015 06:39 PM, aradeonas wrote: > still 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 -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - Or how I learned to stop worrying and love email again

Reply by email

aradeonas
to Michael Schnell

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

http://www.fastmail.com - Email service worth paying for. Try it for free -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/69f42e6b/attachment-0001.html>

Reply by email

Michael Van Canneyt
to aradeonas
aradeonas wrote
Excuse 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.

Reply by email

aradeonas
to Michael Van Canneyt

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

http://www.fastmail.com - Or how I learned to stop worrying and love email again -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/c9966718/attachment.html>

Reply by email

Michael Van Canneyt
to aradeonas
aradeonas wrote
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.

That is as you prefer...
My motto is KISS.

Michael.

Reply by email

Lukasz Sokol
to Graeme Geldenhuys: And the old one that NOBODY adheres too.... Please trim unnecessary quotes - it takes all…
Graeme Geldenhuys wrote 11 lines
On 2015-03-12 16:26, Lukasz Sokol wrote: > Please 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 -

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

Reply by email

Michael Van Canneyt
to aradeonas: OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wrote
OK,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.

Reply by email

Michael Van Canneyt
to Martin Schreiber
Martin Schreiber wrote 5 lines
On Friday 13 March 2015 10:49:43 aradeonas wrote: > OK,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.

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.

Reply by email

Graeme Geldenhuys
to aradeonas: Thank you for explaining,My email serves close all quotes and for this I didn't…
aradeonas wrote
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 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Martin Schreiber
to Michael Van Canneyt: Which goes completely against the very purpose of interfaces. If you need this, you…
Michael Van Canneyt wrote 13 lines
On Fri, 13 Mar 2015, Martin Schreiber wrote: > On Friday 13 March 2015 10:49:43 aradeonas wrote: >> OK,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. 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.

I do not agree.

Martin

Reply by email

Graeme Geldenhuys
to Lukasz Sokol: OOPS :) Apologies for that... BTW The OP's gone a bit overboard now, trimming all the…
Lukasz Sokol wrote
OOPS :) 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Michael Van Canneyt
to Martin Schreiber: I do not agree. Martin
Martin Schreiber wrote 15 lines
On Friday 13 March 2015 11:06:19 Michael Van Canneyt wrote: > On Fri, 13 Mar 2015, Martin Schreiber wrote: >> On Friday 13 March 2015 10:49:43 aradeonas wrote: >>> OK,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. > > 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. > I do not agree.

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.

Reply by email

aradeonas
to Graeme Geldenhuys: 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

http://www.fastmail.com - Choose from over 50 domains or use your own -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/f63d2ec8/attachment-0001.html>

Reply by email

Michael Van Canneyt
to aradeonas
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.

Reply by email

Marcos Douglas
to Michael Van Canneyt: Which goes completely against the very purpose of interfaces. If you need this, you…
Michael Van Canneyt wrote 18 lines
On Fri, 13 Mar 2015, Martin Schreiber wrote: > On Friday 13 March 2015 10:49:43 aradeonas wrote: >> >> OK,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. 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.

+1

Marcos Douglas

Reply by email

Dmitry Boyarintsev
to aradeonas: OK,So in the end how can I cast a Interface to a Object in Lazarus? Ara…
aradeonas wrote
OK,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;
 end

BUT, 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>

Reply by email

Graeme Geldenhuys
to Dmitry Boyarintsev
Dmitry Boyarintsev wrote
BUT, 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: test.pas Type: text/x-pascal Size: 897 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/f3fefffc/attachment.bin>

Reply by email

Dmitry Boyarintsev
to Graeme Geldenhuys

On Fri, Mar 13, 2015 at 10:21 AM, Graeme Geldenhuys <

Show quoted text 5 lines
wrote: 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.

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>

Reply by email

Anthony Walter
to Dmitry Boyarintsev

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>

Reply by email

Graeme Geldenhuys
to Dmitry Boyarintsev: On Fri, Mar 13, 2015 at 10:21 AM, Graeme Geldenhuys < Wait, how can a syntax "work", if…
Dmitry Boyarintsev wrote
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.

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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

aradeonas
to Graeme Geldenhuys

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.

Show quoted text 52 lines
{ IFile } IFile = interface function GetName: string; procedure SetName(AValue: string); property Name: string read GetName write SetName; end; TFileList=specialize TFPGList<IFile>; { TVideoFile } TVideoFile = class(TInterfacedPersistent, IFile) private FName: string; FWidth: integer; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Width: integer read FWidth write FWidth; end; { TAudioFile } TAudioFile = class(TInterfacedPersistent, IFile) private FName: string; FArtist: string; function GetName: string; procedure SetName(AValue: string); public property Name: string read GetName write SetName; property Artist: string read FArtist write FArtist; end; var Form1: TForm1; implementation {$R *.lfm} { TAudioFile } function TAudioFile.GetName: string; begin Result:=FName; end; procedure TAudioFile.SetName(AValue: string); begin FName:=AValue; end; { TVideoFile } function TVideoFile.GetName: string; begin Result:=FName; end; procedure TVideoFile.SetName(AValue: string); begin FName:=AValue; end; var V1,V2:TVideoFile; A1,A2:TAudioFile; Fl:TFileList; begin Fl:=TFileList.Create; V1:=TVideoFile.Create; V1.Name:='V'; V1.Width:=100; A1:=TAudioFile.Create; A1.Name:='A'; A1.Artist:='Art'; FL.Add(V1); FL.Add(A1); V2:=TVideoFile(FL.Items[0]); A2:=TAudioFile(Fl.Items[1]); ShowMessage(V2.Name); ShowMessage(A2.Name); end;

Regards, Ara

http://www.fastmail.com - Access all of your messages and folders wherever you are -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/9f54a84a/attachment.html>

Reply by email

Graeme Geldenhuys
to aradeonas
aradeonas wrote
Graeme 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: test.pas Type: text/x-pascal Size: 1661 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/a5793f8e/attachment.bin>

Reply by email