Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Problem when I store a copy of an object in a new variable

Hi all

Having built an object of a user-defined class and stored it an
element of a public array (all works fine), I now wish to copy that
object to a new element and work on the copy.

For example:


Public StoreObjects(100) as NewClass1

Function CreateCopyClass(OldStoreNumber as integer, NewStoreNumber as
integer)
Set StoreObjects(NewStoreNumber)=StoreObjects(OldStore Number)
CreateCopyClass=NewStoreNumber
End Function

The problem is that while the object seems to have been copied over to
the NewStoreNumber, when later I look to manipulate that new object
within the code it also changes the data in the original object.

What am I doing wrong please?

Thanks very much for your help
Lloyd

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Problem when I store a copy of an object in a new variable

when you use the set command, you are setting a reference to the original
object. Then when you perform a change either using the oldstorenumber or
newstorenumber, you are using the same instance of the class.

You need to create a new instance of the class, copy the attributes between
instances. And create a reference to this new instance.

--
Regards,
Tom Ogilvy


"pinkfloydfan" wrote:

Hi all

Having built an object of a user-defined class and stored it an
element of a public array (all works fine), I now wish to copy that
object to a new element and work on the copy.

For example:


Public StoreObjects(100) as NewClass1

Function CreateCopyClass(OldStoreNumber as integer, NewStoreNumber as
integer)
Set StoreObjects(NewStoreNumber)=StoreObjects(OldStore Number)
CreateCopyClass=NewStoreNumber
End Function

The problem is that while the object seems to have been copied over to
the NewStoreNumber, when later I look to manipulate that new object
within the code it also changes the data in the original object.

What am I doing wrong please?

Thanks very much for your help
Lloyd


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Problem when I store a copy of an object in a new variable

So, I guess I have to set up the new variable/array element and copy
each property separately to it??

Isn't there any easier way to work with a copy of an object?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Problem when I store a copy of an object in a new variable

You can give your class a .Clone method, so it makes a copy of itself.
You still have to write the code to populate each member of the new instance
with the values in the old instance, but at least then it encapsulated in
the class and any user of you class does not need to know how it is
achieved.
As Tom pointed out, Set will increase the reference count on that object by
1, not create a new object.

NickHK

"pinkfloydfan" wrote in message
oups.com...
So, I guess I have to set up the new variable/array element and copy
each property separately to it??

Isn't there any easier way to work with a copy of an object?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Problem when I store a copy of an object in a new variable

thanks a lot Nick, that's very clear as usual

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
store variable keen learner Excel Programming 9 March 8th 06 02:26 PM
store SHAPE-Object on disk Heiko Schmidtke Excel Programming 2 October 25th 05 02:57 PM
Error trap with object variable problem Mike H[_7_] Excel Programming 1 February 6th 04 09:09 AM
Object variable problem ExcelMonkey[_13_] Excel Programming 4 January 23rd 04 08:39 PM
Problem Using an Object Variable in a Class Definition Boxman Excel Programming 2 August 28th 03 03:05 AM


All times are GMT +1. The time now is 09:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"