BdLm
asked on
sharing TObjectlist between Forms / Classes
In my form 1 I create an ObjectList, Form 2 has a Property xObjectList : TObjectList read FObjectList write Fobjectlist, where FObjectList is a local varible of Form 2.
I have been thinking that it is not a good programming style to do something like
Form2. xObjectList := Form1.ObjectList;
Therefore I change Form2 :
now
FObjectList := TObjectList.create;
FObjectList.assign (Form1.ObjectList)
.....
in form1 I did something like :
.....
form2.Form2. xObjectList := Form1.ObjectList; // now with assign inside form 2
form2.ShowModal;
if (Form2.Modalresult = mr ok ) then
begin
Form1.ObjectList := Form2.ObjectList
end;
but the new version is creating an abstract error
q1) what is an abstract error ?
q2) any chance to see the root cause of the abstract error by checking map file or ...
q3) which solution is better in respect to a good programming style ???
--- need no code , just a good explanation ---- !!!!
I have been thinking that it is not a good programming style to do something like
Form2. xObjectList := Form1.ObjectList;
Therefore I change Form2 :
now
FObjectList := TObjectList.create;
FObjectList.assign (Form1.ObjectList)
.....
in form1 I did something like :
.....
form2.Form2. xObjectList := Form1.ObjectList; // now with assign inside form 2
form2.ShowModal;
if (Form2.Modalresult = mr ok ) then
begin
Form1.ObjectList := Form2.ObjectList
end;
but the new version is creating an abstract error
q1) what is an abstract error ?
q2) any chance to see the root cause of the abstract error by checking map file or ...
q3) which solution is better in respect to a good programming style ???
--- need no code , just a good explanation ---- !!!!
Assign is not the best option here. You just need a reference to the first ObjectList which is the way you did it the first time.
Using and assign here will mean that you have to create new objects for the list and assign their properties as well from the source object list
Using the property is fine. You can set it immediately after creating the form
so say
Form2 := TForm2.Create(Application)
try
//assign it here
Form2.ObjectList := ObjectList;
.....
This way you don't need to reassign the objects back to Form1 after they are modified in Form2
A second way is to create a global procedure in Form2 that takes in the ObjectList as a parameter. This is even better since the ObjectList reference is now protected within Form2. You won't need a property
finction ShowForm2(AObjectList: TObjectList): Integer;
implementation
function ShowForm2(AObjectList: TObjectList): Integer;
var
F: TForm2;
begin
F := TForm2.Create(Application)
try
F.FObjectList := AObjectList;
Result := F.ShowModal;
finally
FreeAndNil(F);
end;
end;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bdlm ... can you create a little test sample which shows what you want ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to override this procedure
you will have to overwrite the assign procedure