D
D
Dmitry Afonchenko2015-04-18 18:07:00
Delphi
Dmitry Afonchenko, 2015-04-18 18:07:00

How to pass to Unit1 an object that is in Unit3, avoiding the error: Circular unit reference to 'Unit3′?

Good afternoon, comrades. The following question appeared:
There is a TCell class, which, for convenience, is placed in a separate Unit

Type
    TCell = class

  Public
        X1, Y1: Integer;                  // Координата первой точки
        X2, Y2: Integer;                  // Координата второй точки
        Width_cell: Integer;              // Ширина клетки
        Status: Integer;    // Статус клеточки

        // Пользовательский конструктор
        Constructor CreateUserCell(my_X1, my_Y1, my_X2, my_Y2: Integer);

        Procedure SetColorRed();    // Установить красный цвет
End;

Under Implementation, here is the code:
procedure TCell.SetColorRed;
begin
      Form3.Image1.Canvas.Brush.Color := clRed;
end;

The compiler complains that I am using Form3 in another Unit. This error occurs: Circular unit reference to 'Unit3' The question is how to pass an object from Unit3 to another Unit. Thanks in advance for your replies and comments. I would be grateful for at least a recommendation on how to google.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zed, 2015-04-18
@Indermove

Because you need to abstract in writing code. Rewrite your function like this:

procedure TCell.SetColorRed(ACanvas: TCanvas);
begin
    ACanvas.Brush.Color := clRed;
end;

and call accordingly:
Then your unit will not know anything about the form and, accordingly, it can be removed from uses and break the cyclic dependency.

A
Anton Kuzmichev, 2015-04-18
@Assargin

I don't remember Delphi well, try to move conflicting uses from interface to implementation in one of the modules. Just by the way, in this module, pieces of code of which you have given.

K
kalapanga, 2015-04-18
@kalapanga

How to treat Circular unit reference has already been written to you, but, in my opinion, "something in the conservatory needs to be changed" here. There should not be any references to some "left" (not related to this class) forms, etc. in the class methods. Such a method should not be in your class, but in the form - Form3.SetColorRed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question