Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing an Array from a different Form - is it possible?

Status
Not open for further replies.

kaywarner

Technical User
Jan 8, 2010
24
GB
Hi All,

Thanks for reading the post.

Here is my problem, I have loaded a Text File into a Dynamic Array using one form (TForm1), but then can not read the same array from another form (TForm2).

So what I have at the moment at runtime is the main form opens (TForm2), I can then click to open another form (TForm1), from which I fill the array (Array1) using the Text File, then when I close that off (TForm1 closed)I am unable to access the array form the original form (TForm2).

From Books/Google I'm thinking that I need to use a Pointer - but i'm a newbie and to be honest, i don't understand them very well and i think it will cause havoc.

Any ideas on getting round this??

Many Thanks,

kay


 
I got confused with your Form1 and Form2 names, so I'm going to call them MainForm and OtherForm in MainUnit and OtherUnit, respectively.

MainForm uses OtherForm, but not the other way around.

If Array1 is owned by either Form, remove it so that it is global to OtherUnit. You can now access the array from either form as long as you haven't freed the array, and you only have to read it once.

Here's MainUnit:
Code:
unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
  OtherUnit;   // your other unit

type
  TMainForm = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

end.
And here's OtherUnit:
Code:
unit OtherUnit;

interface

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

type
  TOtherForm = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  OtherForm: TOtherForm;
  Array1: Array of Whatever;  //your array is now global

implementation

{$R *.dfm}

end.
HTH

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top