?
Solved

sharing TObjectlist between Forms / Classes

Posted on 2011-02-16
5
Medium Priority
?
1,198 Views
Last Modified: 2013-11-22
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 ----  !!!!
0
Comment
Question by:BdLm
  • 3
5 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 34905903
an abstract error occurs when calling a procedure which was declared as abstract

you need to override this procedure
you will have to overwrite the assign procedure
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34907993

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;


 
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 1000 total points
ID: 34910309
unless changes have to be done on a copy first
and then the differences indicated ...
then you need assign ... to make that copy

otherwise you just copy the pointer reference
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 34910317
bdlm ... can you create a little test sample which shows what you want ?
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 1000 total points
ID: 34920236
There are a couple of ways of handling this situation.

1) Use the same reference on both forms.

  If the form2 is supposed to edit the list then it could be done without the assignment that you added to form2.  In this case form2 would not be creating or destroying what is in the list.  Anything deleted from the list is destroyed automatically.  If the user cancels form2 then you simply do not update... whatever it is you filled the original list from.  If they click OK then you update whatever the data source for the original list was.  When you are in the code in Form1, after the call to Form2 has returned, the list would be the same one that Form2 was working with (they would both hold the same reference).

2) Use assignments to get the data back and forth.

  If you want to use assignments there is nothing wrong with that.  Remember that Form2 would need to free the list it creates.  When you go to assign it back then you would need Form1 to have assignment capabilities as well.  If I were going to go this route I would create a sub class of TObjectList that handles the assignment.  That way, any two of these objects could assign back and forth.  

Personally I would go with the first route.  It is faster, simpler, and easier to follow.  If it makes you feel better, comment it to show the intent.  I would.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question