[Gambas-user] Copying an Instance of an Object

Benoit Minisini gambas at ...2...
Tue Jul 1 00:02:34 CEST 2003


Le Lundi 30 Juin 2003 19:10, Nigel Gerrard a écrit :
> 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.
>

You must copy the object by hand. The cleanest way is to implement a 
constructor with an optional argument.

' MyClass.class

PUBLIC X AS Integer
PUBLIC Y AS String
PUBLIC Z AS NEW Collection

SUB _new(OPTIONAL hCopy AS MyClass)

  IF hCopy THEN
    X = hCopy.X
    Y = hCopy.Y
    Z = hCopy.Z
  ENDIF

END

Two problems remains:
1) Copying private fields is more complicated.
2) Copying a collection must be done by hand !

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list