Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread TPersistent.Save
Tue, Apr 5 2016 6:14 AMPermanent Link

Matthew Jones

Could TPersistent.Save be made public please?

I was flummoxed by my code not compiling, where I have a list of items
and wanted to save them to a TWriter. Couldn't see a difference between
my code and the persistence sample, other than the definition of the
TCustomer class is in the same unit. So I moved my list code to the
item unit, and it all compiles nicely.

I have also moved the TCustomer definition from the persistence main
form, and it still works, which confuses me even more! How does my code
not work?


unit1:

TJobWorkItem = class (TPersistent)
private
   m_nDisplaySequence : Integer;
g;
public
   constructor Create; override;

published
   property DisplaySequence : Integer read m_nDisplaySequence write
m_nDisplaySequence;

end;


unit2:

function TJobWorkItemList.GetJSON : String;
var               
   nWriteLoop : Integer;
   xWriter : TWriter;
   xItem : TJobWorkItem;
begin      
   xWriter := TWriter.Create(dtfISO8601);
         
   xWriter.Initialize;
   for nWriteLoop := 0 to m_xList.Count - 1 do
   begin
       xItem := WorkItem[nWriteLoop];
       xItem.Save(xWriter); // no matching function found here!!!!
   end;
                
   Result := xWriter.Output;

   xWriter.Free;
end;

--

Matthew Jones
Tue, Apr 5 2016 6:21 AMPermanent Link

Matthew Jones

Matthew Jones wrote:

> Could TPersistent.Save be made public please?

Making it public in my copy makes it work fine. I still don't
understand how the demo works when modified and mine doesn't, but
hopefully someone else will!

--

Matthew Jones
Tue, Apr 5 2016 7:27 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Making it public in my copy makes it work fine. I still don't understand how the demo works when modified and mine doesn't, but hopefully someone else will! >>

It's the special "protected friendship" that the EWB compiler allows, whereby if two classes share the same protected method/property, then it is visible to both (acts as if it is public).

The demo works because the *form* is calling save, and it inherits from TPersistent.  I'm guessing that your TJobWorkItemList class does not.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Apr 5 2016 8:01 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

>  I'm guessing that your TJobWorkItemList class does not.

Bingo! Gaaah! Instant solution to so much.

--

Matthew Jones
Tue, Apr 5 2016 8:24 AMPermanent Link

Matthew Jones

Matthew Jones wrote:

> >  I'm guessing that your TJobWorkItemList class does not.
>
> Bingo! Gaaah! Instant solution to so much.

Err, nope. I just put together a little demo so that others can pick up
an instant solution, pieced from the discussion at
http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&page=1&msg=5813#5813

and the .Save is still not available, even though the list is now
inheriting from TPersistent. The key is that being a generic routine,
and not a member of a class that inherits from TPersistent, it cannot
access the member, even though the item it is calling it on is a
descendant.

My purpose here was to make a more generic routine, which I can then
reduce the number of identical functions with. I guess the answer will
be to make a base list class with that function, and then inherit the
specific list types from that. Back in a while...

--

Matthew Jones
Tue, Apr 5 2016 6:43 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< and the .Save is still not available, even though the list is now inheriting from TPersistent. The key is that being a generic routine, and not a member of a class that inherits from TPersistent, it cannot access the member, even though the item it is calling it on is a descendant. >>

Yes, and that's exactly what I said.  What matters is the context of the *calling method*, and since you're not using a method to make the call, then normal rules apply and protected methods are not accessible to the calling code.

Tim Young
Elevate Software
www.elevatesoft.com
Image