?
Solved

sharing TObjectlist between Forms / Classes

Posted on 2011-02-16
5
Medium Priority
?
1,194 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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