Mr. Smit
Mr. Smit

Reputation: 2532

Delphi: How to access variable in the Parent class from another parent class?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdTCPClient;

type
  TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  public
    Flist : TList;
    property list : TList read Flist write Flist;
  end;

  Tmy_class = class(TThread)
    public
    procedure test;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Tmy_class.test;
begin
  // Error here, can't access the Flist var or list propertie, help !! How to access?
  TForm1(TList).list.Clear;

  // Error
  Form1.list.Clear;

  // Error
  Form1.Flist.clear;

  // HOW ????????
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Flist := FList.Create;
end;

end.

How to get access to "Flist" variable ? Thanks.

Delphi 2010, Indy 10, Win7

Yeap, thats freeking me out: Your post does not have much context to explain the code sections; please explain your scenario more clearly.

Upvotes: 0

Views: 2199

Answers (1)

LU RD
LU RD

Reputation: 34899

You need to address the variable Form1.

Form1.list.clear;

But doing this from a thread is not safe.

Update : compiles fine.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,     Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
 private
    { Private declarations }
    FList : TList;
  public
    { Public declarations }
    property List : TList read FList;
  end;

Type TMyClass = class(TThread)
  Public
    PROCEDURE Test;
end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FList:= TList.Create;  // Look here how to create the list
end;

{ TMyClass }

procedure TMyClass.Test;
begin
  Form1.List.Clear;
end;

end.

But as I warned before, using List directly from a thread is not a good idea.

See also the comment how to create your list.

And yes, the TMyClass has to be properly initiated somewhere.

Upvotes: 1

Related Questions