Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Recursive Classes and the "uses" clause

MarcLimMarcLim Posts: 2Member
Hi, I have a problem that is making me crazy. I have two classes, both have a field of the type of the another. They used to be in the same unit, so there was no problem, but now I'm starting the version 2 and the classes are turning too big to be in the same unit. If I include both units in the "uses" clause I get the "recursive unit reference" error...

How can I get out of this @#1~"% problem? Ideas?

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi, I have a problem that is making me crazy. I have two classes, both have a field of the type of the another. They used to be in the same unit, so there was no problem, but now I'm starting the version 2 and the classes are turning too big to be in the same unit. If I include both units in the "uses" clause I get the "recursive unit reference" error...
    :
    : How can I get out of this @#1~"% problem? Ideas?
    :
    You can put the implementation of the objects in include files. This way the implementation section becomes much shorter:
    [code]
    unit

    interface

    type
    T2 = class;

    T1 = class(TObject)
    procedure SomeProc;
    end;

    T2 = class(TObject)
    procedure SomeProc2;
    end;

    implementation

    {$INCLUDE 'T1Impl.pas'}

    {$INCLUDE 'T2Impl.pas'}

    end.
    [/code]
    and
    [code]
    // Include file for implementation of T1

    procedure T1.SomeProc;
    begin
    end;
    [/code]
    The management of the source becomes somewhat more difficult, because it is spread out over a number of files.
  • netgertnetgert Posts: 331Member
    Another way is not to put the exact type (like TMyControl), but use TObject and then in the implementation part put the other unit into uses-clause and check the type (FMyField is TMyControl)

    example:

    [code]
    unit Unit1

    interface

    uses Classes;

    type
    TFirstObject = class
    FSecondObj: TObject;
    procedure FirstMethod;
    end;

    implementation

    uses Unit2;

    procedure TFirstObject.FirstMethod;
    begin
    if not (FSecondObj is TSecondObject) then
    raise Exception.Create('FFirstObj must be a descendant of TSecondObject');
    end;

    end.
    [/code]

    [code]
    unit Unit2

    interface

    uses Classes;

    type
    TSecondObject = class
    FFirstObj: TObject;
    procedure SecondMethod;
    end;

    implementation

    uses Unit1;

    procedure TSecondObject.SecondMethod;
    begin
    if not (FFirstObj is TFirstObject) then
    raise Exception.Create('FSecondObj must be a descendant of TFirstObject');
    end;

    end.
    [/code]

    : : Hi, I have a problem that is making me crazy. I have two classes, both have a field of the type of the another. They used to be in the same unit, so there was no problem, but now I'm starting the version 2 and the classes are turning too big to be in the same unit. If I include both units in the "uses" clause I get the "recursive unit reference" error...
    : :
    : : How can I get out of this @#1~"% problem? Ideas?
    : :
    : You can put the implementation of the objects in include files. This way the implementation section becomes much shorter:
    : [code]
    : unit
    :
    : interface
    :
    : type
    : T2 = class;
    :
    : T1 = class(TObject)
    : procedure SomeProc;
    : end;
    :
    : T2 = class(TObject)
    : procedure SomeProc2;
    : end;
    :
    : implementation
    :
    : {$INCLUDE 'T1Impl.pas'}
    :
    : {$INCLUDE 'T2Impl.pas'}
    :
    : end.
    : [/code]
    : and
    : [code]
    : // Include file for implementation of T1
    :
    : procedure T1.SomeProc;
    : begin
    : end;
    : [/code]
    : The management of the source becomes somewhat more difficult, because it is spread out over a number of files.
    :
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

  • KoppisKoppis Posts: 68Member
    : Hi, I have a problem that is making me crazy. I have two classes, both have a field of the type of the another. They used to be in the same unit, so there was no problem, but now I'm starting the version 2 and the classes are turning too big to be in the same unit. If I include both units in the "uses" clause I get the "recursive unit reference" error...
    :
    : How can I get out of this @#1~"% problem? Ideas?
    :

    You can use uses-clause in 2 different places. If you had read the help files, you would know this. One of those 2 Units must have their uses clause BEFORE "implementation" word and the another must have it AFTER it.
    See this:

    Unit Unit1;
    Uses Unit2;
    .
    .
    .
    [red]implementation[/red]
    ---
    Unit Unit2;
    .
    .
    .
    [red]implementation[/red]

    Uses Unit1;
Sign In or Register to comment.