Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inaccessible members in nested class - WHY?

TTest= class
public abc:Integer;
type
  TTest2 = class
    procedure test;
  end;
end;

procedure TTest.TTest2.test();
begin
  abc:=5; //'Inaccessible here'
end;

I get 'instance member abc not accessible here'.

The situation is serious. I have to use a thread, but since TThread is abstract, own class should inherit it. In my case the class that needs the thread already inherits another class that cannot inherit TThread... so this is not possible. My scenario is the nested class to inherit TThread and below I declare it. But the Execute(); method must use members of its parent class.

How do I do it? Why this error?

like image 340
JBA Avatar asked Dec 10 '22 10:12

JBA


1 Answers

The inner class is exactly that: a separate class. Only its name scope is nested inside of the parent class. You can construct an instance of the inner class without an instance of the parent class.

If you make the abc member a class variable, then you can access it from the inner class because class vars have global storage independent of the object instances.

Otherwise, you will need to pass an instance of the parent class into the nested class in order for the nested class to write to the parent instance's fields.

like image 156
dthorpe Avatar answered Dec 15 '22 00:12

dthorpe