views:

281

answers:

1

I am trying to access a global variable on a form that is one in an Array of Forms, I have tried using this:

max_forms := 3

setlength(form_array, max_forms);

form_array[1] := frm1;
form_array[2] := frm2;

if current_form > 0 then
begin
  form_array[current_form].fNumber := Number;
  form_array[current_form].ShowModal;
end;

The above does not work obviously. Any help would be greatly appreciated.

+1  A: 

I guess the problem is that fNumber has private access.

Depending on the purpose of this field, one solution could be creating a property to gain write access:

property Number: Integer read FNumber write FNumber;

Then you can do the assignment:

form_array[current_form].Number := Number;

About global variables:

If this is in fact the problem and FNumber is the "global" variable you are talking about, then you are using the wrong words. FNumber is a field and belongs to a form. Form members are not global.

Look at the source of your form. If it has been generated by the Delphi IDE you'll find a variable declaration below it:

end; // End of TForm1

var
  Form1: TForm1;

implementation

Form1 is a real global variable, because it exists in the interface of a unit and outside of any class and you can access from anywhere (not a good thing in general) as for FNumber you first need access to a form instance.

PS: I don't know what exactly you are trying to achive, but perhaps you can take a look at Screen.Forms which provides a list of the active forms. That might be better suited than a custom list.

DR
Ok so I have'nt really used property before (I know, I'm still a newbie) any ideas on which unit I should set it up on?
AJ
It has to be in the same class as the field you access, in your case in the form and in the public part.
DR
Explanation of the property keyword: http://www.delphibasics.co.uk/RTL.asp?Name=Property
DR
Thanks will read up on that.
AJ