[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sharing TObjectlist between Forms / Classes

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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