简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM