Creating a new instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ffa@newsgroup.nospam

    #1

    Creating a new instance

    Hi Guys

    If I do something like:

    Dim newClass As Customer
    newClass = oCustomer (an existing Customer object)

    Then when I make a change in oCustomer, the change is also applied to
    newClass.

    Is there some quick way to create a new instance of oCustomer that is an
    independant object. I want to use this for some undo functionallity and
    change logging.

    Thanks


  • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

    #2
    RE: Creating a new instance

    I don't know if this is necessarily the quickest way. I usually implement
    ICloneable, and in the Clone method, serialize the current object then
    deserialize it to anothier instance to return from the method. This would
    only be for fairly complicated classes though, as if there are only a few
    properties, its not worth it.

    "ffa@newsgroup. nospam" wrote:
    Hi Guys
    >
    If I do something like:
    >
    Dim newClass As Customer
    newClass = oCustomer (an existing Customer object)
    >
    Then when I make a change in oCustomer, the change is also applied to
    newClass.
    >
    Is there some quick way to create a new instance of oCustomer that is an
    independant object. I want to use this for some undo functionallity and
    change logging.
    >
    Thanks
    >
    >
    >

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: Creating a new instance

      ffa@newsgroup.n ospam wrote:
      Hi Guys
      >
      If I do something like:
      >
      Dim newClass As Customer
      newClass = oCustomer (an existing Customer object)
      >
      Then when I make a change in oCustomer, the change is also applied to
      newClass.
      >
      Is there some quick way to create a new instance of oCustomer that is an
      independant object. I want to use this for some undo functionallity and
      change logging.
      >
      Thanks
      >
      There is no quick way to copy an object, as an object can be too
      complicated for an automatic copy. It's up to the object itself to
      implement cloning.

      This can be done by implementing the IClonable interface, as
      ModelBuilder suggested. Another common way of doing it is to make a
      constructor that takes an object as parameter, so that you would do:

      Dim newClass as New Customer(oCusto mer)

      The constructor would just copy the relevant data from the passed object
      to make itself a copy of it.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...