I want to implement an interface, but I cannot declare it from TInterfacedObject
, and in this case the documentation says that I must implement QueryInterface
, _AddRef
, _Release
. But I am not sure if this is mandatory. I haven't really worked with interfaces so far... They say something about COM objects, but I don't even know what that means. I never intentionally used COM objects, maybe only if they are included in some pieces of code which I took from the internet. I tried an example (see below) and it's working without implementing those 3 methods. So...
- How should I know if I must implemet them ?
- If I must implement them, how can I do it ? I don't know nothing about those methods should do. Should I copy the implementation from
TInterfacedObject
? (In fact, it has more than 3... those extra ones must be copied too ?)
Thanks !
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
IShellInterface = interface
['{55967E6B-5309-4AD0-B6E6-739D97A50626}']
procedure SetPath(const APath: String);
function GetPath: String;
end;
TDriveBar = class(TCustomPanel, IShellInterface)
private
FDriveLink: IShellInterface;
FPath: String;
public
procedure SetPath(const APath: String);
function GetPath: String;
property DriveLink: IShellInterface read FDriveLink write FDriveLink;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
DriveBar1, DriveBar2: TDriveBar;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TDriveBar.SetPath(const APath: String);
begin
FPath:= APath;
Caption:= APath;
end;
function TDriveBar.GetPath: String;
begin
Result:= FPath;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown:= True;
DriveBar1:= TDriveBar.Create(Form1);
DriveBar1.Parent:= Form1;
DriveBar1.SetBounds(20, 20, 250, 40);
DriveBar1.SetPath('Drive Bar 1');
DriveBar2:= TDriveBar.Create(Form1);
DriveBar2.Parent:= Form1;
DriveBar2.SetBounds(20, 80, 250, 40);
DriveBar2.SetPath('Drive Bar 2');
DriveBar1.DriveLink:= DriveBar2;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DriveBar1.DriveLink.SetPath('D:\Software\Test');
Caption:= DriveBar2.GetPath;
end;
end.