[Gambas-user] How to clone an object?

Brian G brian at westwoodsvcs.com
Fri Apr 24 22:10:56 CEST 2020



Thank You 
Brian G 


From: "Jussi Lahtinen" <jussi.lahtinen at gmail.com> 
To: "Gambas mailing list" <user at lists.gambas-basic.org>, "Benoît Minisini" <g4mba5 at gmail.com> 
Sent: Thursday, April 23, 2020 3:03:07 PM 
Subject: Re: [Gambas-user] How to clone an object? 




You need example from Benoit. As it is implemented only recently and I haven't seen any documentation for it. 



Oh, actually there is documentation for it now... [ http://gambaswiki.org/wiki/lang/special/write?nh | http://gambaswiki.org/wiki/lang/special/write?nh ] 
In the special method you need to write the private variables to the stream. 

Benoit, is it possible to have Object.Copy(), which would work by using _read method? 


Jussi 

example for clone 

myclass 

_write(mfile as file) 

.. write all data fileld to clone 
end 

_read(mfile as file) 

.. read all fields cloned 
end 

public sub clone() as object 

dim msize as integer 
dim myobject as variant 
dim mem as pointer 
dim mfile as file 

mFile = Open Null For Write 
Try Write #mfile, me As Object 
If Error Then 
Error.Raise("Unable to write object to memory," & Error.text) 
Endif 
msize = Seek(mFile) 
Close mFile 

mem = alloc(msize) 

mfile = memory mem for write 
Try Write #mfile, me As Object 
If Error Then 
Error.Raise("Unable to write object to memory," & Error.text) 
Endif 
close mfile 
mfile = memory mem for read 
Try MyObject = Read #mFile As Object 
If Error Then error.raise( "Object read failed") 
Close mFile 
free(mem) 

Return MyObject 
end 


hope this helps 
Brian G 



----[ http://gambaswiki.org/wiki/doc/netiquette ]---- 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20200424/6eaaae93/attachment.html>


More information about the User mailing list