How to create an instance of two classes at the same time?

This is my problem - in short:

var c1:TClass1;
c2:TClass2;
begin
  c1 := c1.Create;
  c2 := c2.Create; //<<Exception;
end;

Both classes inherit TObject. If they are not inherited, I cannot use the debugger in the class, so I need to use TObject.

My real problem is that I have to instantiate a second class inside a function in an instance of the first class. I cannot find a way to free an instance of the first class while I'm inside it.

It seems I cannot have more than one class that inherits TObject, is there a problem?

How to fix my code, any suggestions?

Thanks in advance!

+3
source share
3 answers

The correct syntax should be:

C1 := TClass1.Create;
C2 := TClass2.Create;
+16
source

To extend the answer to skamradt:

. . , . , ?

+4

Do not forget that the designers perform two tasks.

var c1: TClass1;
c1 := TClass1.Create;

build a new instance of type TClass1, and

c1.Create;

will reinitialize c1 - all instructions in the constructor will be executed, but the constructor will not return a new instance.

0
source

Source: https://habr.com/ru/post/1712625/


All Articles