Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing self as parameter in Delphi [duplicate]

Tags:

delphi

pascal

I would like to pass "self" as parameter to a method of another class (in a different unit). However the type of the first class is unknown in the second one, because I can't put the first unit into the uses section of the second unit. So I define the parameters type as pointer but when I try to call a method from the first class the Delphi 7 parser tells me that the classtyp is required.

So how should I solve this problem?

like image 377
ltsstar Avatar asked May 06 '26 00:05

ltsstar


1 Answers

By making the class known in the implementaion part you can cast the given reference.

unit UnitY;

interface
uses Classes;
type
    TTest=Class
       Constructor Create(AUnKnowOne:TObject);
    End;



implementation
uses UnitX;
{ TTest }

constructor TTest.Create(AUnKnowOne: TObject);
begin
    if AUnKnowOne is TClassFromUnitX then
      begin
         TClassFromUnitX(AUnKnowOne).DoSomeThing;
      end
    else
      begin
         // .... 
      end;
end;

end.
like image 118
bummi Avatar answered May 08 '26 23:05

bummi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!