I have to solve next problem:
Form23:
public
{ Public declarations }
FormsArray : array of TForm24;
end;
Procedure Create_form;
begin
SetLength(FormsArray, Length(FormsArray)+1);
FormsArray[Length(FormsArray)-1] := TForm24.Create(Self);
end;
Form24:
public
end;
var
UniqueValue : Array of ShortString;
Procedure Fill_Unique;
var
tmp1 : Longint;
begin
SetLength(UniqueValue, 256);
for tmp1 := 0 to Length(UniqueValue)-1 do
begin
UniqueValue[tmp1] := IntToStr(tmp1);
end;
end;
Procedure OnButtonClick(Sender);
begin
Fill_Unique;
end;
When i have one form Form24 and i fill with some values, then it is ok.
When i have two forms Form24 (FormsArray[0] and FormsArray[1]) and i change UniqueValue in one form, then i have that values in two forms.
i.e.
I create FormsArray[0] and FormsArray[1]
When I click button on FormsArray[0]:
FormsArray[0] – UniqueValue[…] = ‘1,2,3,4,5,6,7,8,9…’;
FormsArray[1] – UniqueValue[…] = ‘1,2,3,4,5,6,7,8,9…’;
When I click button on FormsArray[1]:
FormsArray[0] – UniqueValue[…] = ‘1,2,3,4,5,6,7,8,9…’;
FormsArray[1] – UniqueValue[…] = ‘1,2,3,4,5,6,7,8,9…’;
When i change code to:
Form24:
public
UniqueValue : Array of ShortString;
end;
and I click button on FormsArray[1] then i have:
FormsArray[0] – UniqueValue[…] = ”;
FormsArray[1] – UniqueValue[…] = ”;
UniqueValue is empty.
I need to have independent arrays in every forms Form24 i have created (different UniqueValue in every forms i create).
How to do this? What i do wrong?
Thanks for any help.
SOLVED !
I got -1 for solution what i write here. Then will be no solution. Search for yourself.
It sounds like you’ve already solved it. Make
UniqueValuebe a member of the form class. Put it in thepublicsection of the class declaration, for example.The first code you showed has the array as a global variable, which is of course shared by all instances of your form class, as well as everything else in your program. You’re probably confused thinking that anything declared in the same file as the form class somehow “belongs to” that class, but if you think that, you’re mistaken. To make something belong to a class, it should be declared inside that class, not just somewhere in the same unit file.
It looks like you’ll probably want to make
Fill_UniqueandOnButtonClickbe members of the form class, too. In the code you showed, they’re standalone procedures, so they have no reference to whatever form they’re supposed to work on. That means they can’t refer toUniqueValuebecause they won’t know whichTForm24instance’s field to operate on.