tags:

views:

252

answers:

3

Hello,

This is my problem - shortly:

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

Both classes inherit from TObject.If they don't inherit then I can't use the debugger in the class so I have to use TObject.

My real problem is that I have to create the instance of the second class inside a function in the instance of the first class.I can't find a way to free the instance of the first class while i'm inside it.

It seems I can't have more than one class that inherits from TObject,is that the problem?

How do I fix my code,any suggestions?

Thanks in advance!

+15  A: 

The proper syntax should be:

C1 := TClass1.Create;
C2 := TClass2.Create;
skamradt
Indeed. The question is not about creating two instances at the same time. It's about managing to create any instance at all.
Rob Kennedy
+4  A: 

To expand on skamradt's answer:

You are attempting to use your classes before creating them. Internally, classes are pointers to the data in the class. Thus you are dereferencing an unassigned pointer. Is it any wonder your code goes boom?

Loren Pechtel
A: 

Don't forget that constructors do two jobs.

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

will construct a new instance of type TClass1, while

c1.Create;

will re-initialise c1 - all the statements in the constructor will execute, but the constructor won't return a new instance.

Frank Shearar