Uses Unit - Out of memory

Asked

Viewed 371 times

3

My class Tlog inherits from the Tbasemodel class:

uses
  BaseModelo;

TNFLog = class(TBaseModelo)
...
end;

But my Tbasemodel class needs to have a Tlog-type attribute:

uses
  Log;

TBaseModelo = class(TInterfacedPersistent)
public
  property Log: TLog read FLog write FLog;
end;

When indicating the Log in Basetemplate uses, when compiling, the error occurs "Out of memory".

From what I understood it would be a cyclic error (loop) correct? How to get around this?

2 answers

2

To get around this Voce has some alternatives. The most used is to declare both in the same Unit, using Foward Declaration

TLog = class;

TBaseModelo = class(TInterfacedPersistent)
public
  property Log: TLog read FLog write FLog;
end;

TLog = class(TBaseModelo)
...
end;

Another amendment, and is the one I recommend, is to transform the definition into an interface

unit LogIntf

type
  ILog = interface(IInterface)
  ['{8A897A31-7457-46C3-986A-17786CA11404}']
    procedure Logar(const LogMsg: string);
  end;

Hence in the class of Tbasemodel you can use:

unit BaseModelo;

uses
  LogIntf;

type
    TBaseModelo = class(TInterfacedPersistent)
    private
      FLog: ILog
    public
      property Log: ILog read FLog write FLog;
    end;

And in the class of Tlog can do:

uses
  LogIntf, BaseModelo;

type
    TLog = class(TBaseModelo, ILog)
    ...
    end;

0

This is because you are using a Unit that makes uses of Unit that is using it that by the way makes uses of Unit that also makes uses of Unit that is giving uses to it.

Complicated right? Yeah, that’s how Delphi thinks... Bad right?

I suggest you declare Tbasemodel and Tlog in the same Unit. Delphi is an old man, go easy on him.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.