Thema: Delphi Problem mit Interfaces

Einzelnen Beitrag anzeigen

hoika

Registriert seit: 5. Jul 2006
Ort: Magdeburg
8.270 Beiträge
 
Delphi 10.4 Sydney
 
#1

Problem mit Interfaces

  Alt 2. Aug 2006, 18:02
Hallo,

ich verzweifle gerade an Interfaces.

Das Problem ist die Stelle // XXX //
Danach wird der Speicher der lokalen Varaible TestId überschrieben.

Ich denke, der Fehler liegt im GetInterface.
Nur wie soll ich denn sonst ein Interface auf die Klasse bekommen ???.


Unteres Bsp führt zum Überschreiben der lokalen Variabel TestCode.
Was mache ich falsch ?

Ich will von dieser Basisklasse weg,
also bitte keine "pack das property doch in die Basisklasse"

Danke
Heiko

Delphi-Quellcode:
type
  IResource = interface
    function GetResType: Integer;

    property iResType: Integer
      read GetResType
  end;

type
  TBaseRes = class(TInterfacedClass)
    function GetInterface: IResource; virtual; abstract;
  end;

// Res1

type
  TRes1 = class(TBaseRes, IRes)
    function GetInterface: IResource; override;
    function GetResType: Integer;
  end;

function TRes1.GetInterface: IResource;
var
  IntRes: IResource;
begin
  IntRes:= Self;
  Result:= IntRes;
end;

function TRes1.GetResType: Integer;
begin
  Result:= 1;
end;

// Res2

type
  TRes2 = class(TBaseRes, IRes)
    function GetInterface: IResource; override;
    function GetResType: Integer;
  end;

function TRes2.GetInterface: IResource;
var
  IntRes: IResource;
begin
  IntRes:= Self;
  Result:= IntRes;
end;

function TRes2.GetResType: Integer;
begin
  Result:= 2;
end;

// problem code

var
  iResItem : Integer;
  ResItem : TBaseRes;
  IntRes : IResource;
  TestId : Integer;
begin
  TestId:= 100;

  for iResItem:= 0 to ResList.Count-1 do
  begin
    ResItem:= ResList[iResItem];

    IntRes:= ResItem.GetInterface;


  // XXX //
    if IntRes.iResType=1 do
    begin
      blaaa


   // TestId wurde verändert !!
  end;
Heiko
  Mit Zitat antworten Zitat