On 2015-03-13 16:50, 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/
program test;
{$mode objfpc}{$H+}
// Which interface type are we using. Enable for COM interface
{$define UseCom}
{$ifdef UseCom}
{$interfaces com}
{$else}
{$interfaces corba}
{$endif}
uses
Classes,
SysUtils;
type
IMyInterface = interface
procedure SetCaption(AValue: string);
function GetCaption: string;
property Caption: string read GetCaption write SetCaption;
end;
TMyObject = class({$ifdef UseCom}TInterfacedObject{$else}TObject{$endif}, IMyInterface)
private
FCaption: String;
procedure SetCaption(AValue: string);
function GetCaption: string;
public
constructor CreateCustom(ACaption: string);
procedure SayHello;
property Caption: string read GetCaption write SetCaption;
end;
procedure TMyObject.SayHello;
begin
writeln('Hello from the object');
end;
procedure TMyObject.SetCaption(AValue: string);
begin
FCaption := AValue;
end;
function TMyObject.GetCaption: string;
begin
Result := FCaption;
end;
constructor TMyObject.CreateCustom(ACaption: string);
begin
Create;
FCaption := ACaption;
end;
var
LIntfRef: IMyInterface;
LObj: TMyObject;
begin
{ Create an interfaced object and extract an interface from it. }
LIntfRef := TMyObject.CreateCustom('My Caption');
writeln('via interface: ' + LIntfRef.Caption);
{ Cast the interface back to the original object. }
// LObj := TMyObject(LIntfRef);
LObj := LIntfRef as TMyObject;
if Assigned(LObj) then
begin
writeln('Pass: we got the object');
LObj.SayHello;
writeln('via object instance: ' + LObj.Caption);
end
else
writeln('Fail: we could not get the object instance');
end.
--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus