[Gambas-user] Copying an Instance of an Object

Nigel Gerrard nigel at ...38...
Mon Jun 30 19:10:41 CEST 2003


I maybe missing something here but I am trying make a copy of an Object for holding in a collection
whilst keeping the original object for manipulation.

e.g.

PUBLIC MyClass AS Something
PUBLIC MyCollection AS Collection
PUBLIC MyOther AS Something


PUBLIC SUB MAIN()
     DIM i AS Integer
     MyOther = NEW Something
     MyCollection = NEW Collection

     FOR i = 1 TO 7
         MyClass = NEW Something
         MyOther.ID = i
         MyOther.Stuff = "Stuff - " & CStr(i)
         MyClass = MyOther             ' This is where I would like to have MyOther Copied into MyClass...but I don't want to work through all
                                                   '  the other elements.  Something like = NEW Something(MyOther) would have been nice :-)
         MyCollection.Add(MyClass, CStr(MyClass.ID))
     NEXT

     FOR EACH MyClass IN MyCollection
         PRINT "ID                 = "; MyClass.ID
         PRINT "Stuff             = "; MyClass.Stuff 
     NEXT

END


.....And of course, the output comes out...
ID = 7
Stuff = Stuff-7
ID = 7
Stuff = Stuff-7
.etc.


Am I missing something obvious for creating a copy of the object? I'm trying to think back to C++ and operator overloading :-)


Nigel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20030630/53b690e0/attachment.html>


More information about the User mailing list