[Gambas-user] Copying an Instance of an Object

Philip A. Chapman pchapman at ...129...
Mon Jun 30 20:04:24 CEST 2003


Nigel,

The code you have, the line:

MyClass = MyOther

simply assigns a reference to MyOther class to the MyClass variable. 
All you have done is copied the pointer reference, not the class that it
points too.  I do not think you can copy the object without instinating
a new object reference and manipulating it.  This is not what you
wanted, but perhaps you can make it a little cleaner and reusable by
putting it in the class itself:


In Something class module:

  PUBLIC FUNCTION Clone() AS Something
  
    DIM oNewSomething AS Something
  
    oNewSomething = NEW oNewSomething
    oNewSomething.ID = ME.ID
    oNewSomething.Stuff = ME.Stuff
  
    RETURN oNewSomething
  END

With the above function added to the Something class, you can do the
following in the Main method:

  MyCollection = NEW Collection
  
  FOR i = 1 TO 7
    MyOther = NEW Something  ' Not quite what you had before
    MyOther.ID = i
    MyOther.Stuff = "Stuff - " & CStr(i)
    MyClass = MyOther.Clone()
    MyCollection.Add(MyClass, CStr(MyClass.ID))
  NEXT

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


On Mon, 2003-06-30 at 12:10, Nigel Gerrard wrote:
> 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
-- 
Philip A. Chapman

Application Development:
Java, Visual Basic (MCP), PostgreSQL, MySQL, MSSQL
Linux, Windows 9x, Windows NT, Windows 2000, Windows XP
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20030630/b82ef207/attachment.sig>


More information about the User mailing list