Solved

sharing TObjectlist between Forms / Classes

Posted on 2011-02-16
5
1,179 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 37

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:ewangoya
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 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 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 37

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 250 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now