gettingstarted
[Top] [All Lists]

Re: Assigning one Object to Another

To: Getting Started <gettingstarted at lists dot realsoftware dot com>
Subject: Re: Assigning one Object to Another
From: Phil Mobley <phil at mobleybros dot com>
Date: Wed, 28 Jul 2004 09:18:22 -0700
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <4107D8AC dot 8123 dot BD940D1 at localhost>
On Jul 28, 2004, at 8:47 AM, Jon wrote:

I'm sure I saw a reference to this a day or two ago, but did not take much notice of it. Having searched the archives I can't find it.

I have a custom class which is instantiated as PilotData. There is one instance and it stores preference and other user information. I need to make a copy of it before I carry out a specific operation so that if the operation fails I can roll back. I did this by instantiating a second object from the class and called it TempPilotData.

Before the operation I did:

TempPilotData=PilotData

But this does not work since it seems that the equivalence at the start points to the original object and does not create a copy.

Right, you are just copying the memory address of the PilotData to TempPilotData.

Instead what you need is a copy constructor. To do this, create a Constructor method which takes an instance of your base class as a property:

  Sub Constructor( orig as MyClass)

Then you would just copy all of the properties contained in the class to the new object...

  myInt = orig.myInt
  myDouble = orig.myDouble
  myString = orig.myString

But again you need to be careful if you have objects as properties, otherwise you will run into the same problem as before:

  p = NewPicture(orig.p.width, orig.p.height, orig.p.depth)
  p.graphics.drawpicture orig.p, 0, 0

  mb = NewMemoryBlock(orig.mb.size)
  mb.StringValue(0, mb.Size) = orig.mb.StringValue(0, mb.Size)

And so on...

Then when you want to make a copy of the object within your code, you just write:

  TempPilotData = New MyClass(PilotData)

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>