Solved

sharing TObjectlist between Forms / Classes

Posted on 2011-02-16
5
1,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

689 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