Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread TPersistent Save / Load accessibility
Thu, Mar 23 2017 12:04 PMPermanent Link

Matthew Jones

Could these procedures be made public please? I realise they are sort of intended to be protected, but I realised that rather than write a lot of code to clone an object, which has a hierarchy so lots to duplicate, it would be faster and more re-usable to save an object to JSON and then load it again. Only then I hit the protected status of these two functions.

Or is there a way to hack access? [fx: tappety tap, surely the old Delphi hack doesn't work? Aargh!]

I present to you a generic clone function, below. No need to make them accessible...

--

Matthew Jones



unit uCloneUtils;

interface

uses WebCore;
                                                             
type

TPersistentClass = class of TPersistent;

function CloneObject(xObject : TPersistent; classType : TPersistentClass) : TObject;

implementation

type

TPersistentHack = class (TPersistent)
private
public
end;

function CloneObject(xObject : TPersistent; classType : TPersistentClass) : TObject;
var
   xReader : TReader;
   xWriter : TWriter;
   szJSON : String;
begin               
   Result := classType.Create;

   xWriter := TWriter.Create(dtfISO8601);
   try
       xWriter.Initialize;
       TPersistentHack(xObject).Save(xWriter);
       szJSON := xWriter.Output;
   finally
       xWriter.Free;
   end;

   xReader := TReader.Create(dtfISO8601);
   try
       xReader.Initialize(szJSON);
       TPersistentHack(Result).Load(xReader);
   finally
       xReader.Free;
   end;
end;

end.
Tue, May 29 2018 9:00 AMPermanent Link

Ralf Mimoun

Matthew,

does this code work with the current version of EWB? I get an error that the property "beginupdate" can't be called for an undefined or null reference. I try to clonea TBasicPanel with a TImage, three TLabel and a TButton.


"Matthew Jones" wrote:

Could these procedures be made public please? I realise they are sort of intended to be protected, but I realised that rather than write a lot of code to clone an object, which has a hierarchy so lots to duplicate, it would be faster and more re-usable to save an object to JSON and then load it again. Only then I hit the protected status of these two functions.

Or is there a way to hack access? [fx: tappety tap, surely the old Delphi hack doesn't work? Aargh!]

I present to you a generic clone function, below. No need to make them accessible...

--

Matthew Jones



unit uCloneUtils;

interface

uses WebCore;
                                                             
type

TPersistentClass = class of TPersistent;

function CloneObject(xObject : TPersistent; classType : TPersistentClass) : TObject;

implementation

type

TPersistentHack = class (TPersistent)
private
public
end;

function CloneObject(xObject : TPersistent; classType : TPersistentClass) : TObject;
var
   xReader : TReader;
   xWriter : TWriter;
   szJSON : String;
begin               
   Result := classType.Create;

   xWriter := TWriter.Create(dtfISO8601);
   try
       xWriter.Initialize;
       TPersistentHack(xObject).Save(xWriter);
       szJSON := xWriter.Output;
   finally
       xWriter.Free;
   end;

   xReader := TReader.Create(dtfISO8601);
   try
       xReader.Initialize(szJSON);
       TPersistentHack(Result).Load(xReader);
   finally
       xReader.Free;
   end;
end;

end.
Tue, May 29 2018 9:58 AMPermanent Link

Matthew Jones

Ralf Mimoun wrote:

> does this code work with the current version of EWB? I get an error that the property "beginupdate" can't be called for an undefined or null reference. I try to clonea TBasicPanel with a TImage, three TLabel and a TButton.

It was from a recent version, so should work, in general. I was cloning my own classes, not UI, so not sure. However, the undefined implies some other aspect not being initialised, so you may need to look at the code for those items. First step is to run it in the Chrome debugger and see which part of the code is causing the fail, and then you can see what it is doing and work out if it is fixable.

--

Matthew Jones
Tue, May 29 2018 1:31 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Could these procedures be made public please? I realise they are sort of intended to be protected, but I realised that rather than write a lot of code to clone an object, which has a hierarchy so lots to duplicate, it would be faster and more re-usable to save an object to JSON and then load it again. Only then I hit the protected
status of these two functions. >>

EWB has a different scoping rules for protected class members, when compared to Delphi.  As long as the class calling the methods is also a TPersistent descendant class, it should be able to access those methods.

Tim Young
Elevate Software
www.elevatesoft.com
Image