Class objects in two collections

Class objects in two collections

mdhutchinson
Advisor Advisor
385 Views
4 Replies
Message 1 of 5

Class objects in two collections

mdhutchinson
Advisor
Advisor
At one point in my application I have two collections which initially are identical ... they are both initiated and filled from records in a csv file within a single procedure. The content of each collection are class objects.

The idea is that one of these collections is intended to be untouched by my application. The other is a working copy which my application (and the user) edits as well as adds new objects. The purpose for the pristine copy is for comparison only so that my application can determine if the objects properties have changed from there initial value.

The problem I am having is that when my application edits the working copy... the pristine copy which isn't to be edited changes.

I have given specific variable names to the objects taken from the working copy which are edited. I set that variable name to the item from the working collection and edit it's properties. The props change correctly... but the matching object found in the pristine copy also changes but should not.

Why might this be happening?
The code is large so I don't see that it would be possible to show my code... and admittedly, I am still cutting my baby teeth on VBA beginning with this app - so please go easy on me.
0 Likes
386 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
without seeing some code there is no way to diagnois your condition.

wrote in message news:5741509@discussion.autodesk.com...
At one point in my application I have two collections which initially are
identical ... they are both initiated and filled from records in a csv file
within a single procedure. The content of each collection are class objects.

The idea is that one of these collections is intended to be untouched by my
application. The other is a working copy which my application (and the user)
edits as well as adds new objects. The purpose for the pristine copy is for
comparison only so that my application can determine if the objects
properties have changed from there initial value.

The problem I am having is that when my application edits the working
copy... the pristine copy which isn't to be edited changes.

I have given specific variable names to the objects taken from the working
copy which are edited. I set that variable name to the item from the working
collection and edit it's properties. The props change correctly... but the
matching object found in the pristine copy also changes but should not.

Why might this be happening?
The code is large so I don't see that it would be possible to show my
code... and admittedly, I am still cutting my baby teeth on VBA beginning
with this app - so please go easy on me.
0 Likes
Message 3 of 5

Anonymous
Not applicable
It is most likely, the you added the same instance of your class object into
the two collection. You must distinguish object variable and object instance
itself. You cannot see/touch an object, you only use a variable to refer to
it (reference, or pointer). Sample:

'Declare variable (reference, to 2 types of objects: MyClass.MyClass2)
'But they do not refer to /point to any thing. they are not object itself
'So they are "Nothing"
Dim obj1 As MyClass1
Dim obj2 As MyClass1

''Create object instance and have the variables to point to it
''The object instance can only be reached by a reference/pointer - variables

Set obj1=New MyClass1
'"New" creates an object, "Set" makes obj1 point to the newly created object
instance
'Object instance stays in momey somewhere, the variable pointing to it
'stays in its only memory, but has the value pointing to the object. You can
have
'many variables pointing to the same object. However, once there is no
variable/reference pointing to
'an object, the object will be cleared.
'So, Set obj1=Nothing does not guarantee the object is cleared, it just cut
off the reference to an object.

Set obj2=obj1
'obj2 is another reference, pointing to the same object instance as obj1
does.
'Whatever you do via obj1, or via obj2, the effect all falls on the same
object instance.

'in your case

Dim col1 As colelction
Dim col2 as Collection
...

'now the same object is added to two collections, because the references
point to the same object i nstance.
'So, whatever you do via obj1 or obj2, the result shows on the same object,
regardless which collection the object is in.
col1.Add obj1
col2.Add obj2

''You need
Set obj1=New Class1

'Set its initializing proiperties here

col1.Add obj1

''repeat to add more...

Set obj2=New Class1

'Set its initializing properties, the same as obj1, but now they are not
pointing to the same object,
'although the objects are having the same property values

col2.Add obj2

''repeat to add more....

Now you have two collection of objects with the same inital property values.
Do some thing on one will not affect objects in the other collection



wrote in message news:5741509@discussion.autodesk.com...
At one point in my application I have two collections which initially are
identical ... they are both initiated and filled from records in a csv file
within a single procedure. The content of each collection are class objects.

The idea is that one of these collections is intended to be untouched by my
application. The other is a working copy which my application (and the user)
edits as well as adds new objects. The purpose for the pristine copy is for
comparison only so that my application can determine if the objects
properties have changed from there initial value.

The problem I am having is that when my application edits the working
copy... the pristine copy which isn't to be edited changes.

I have given specific variable names to the objects taken from the working
copy which are edited. I set that variable name to the item from the working
collection and edit it's properties. The props change correctly... but the
matching object found in the pristine copy also changes but should not.

Why might this be happening?
The code is large so I don't see that it would be possible to show my
code... and admittedly, I am still cutting my baby teeth on VBA beginning
with this app - so please go easy on me.
0 Likes
Message 4 of 5

mdhutchinson
Advisor
Advisor
'It is most likely, the you added the same instance of your class object into the two collections.'

Yes... that is what happened.

What I learned today...
Instantiate a class object and add it to two collections.
... editing an object fetched from one collection will effect the object in the second collection.

I wanted to initially have two collections that were identical.
One used for reference purposes only and the other for editing. Comparison with the object when edited to the object in the reference collection would alert my app that the properties changed from their initial values.

Previously, I had created properties in the class module that would indicate that a value change. This was working well except it couldn't see if it changed back to the original value.
The only way I could see that I could have this functionality is if I initially create two collections; a reference collection and a working collection. This now seems to be working as I wanted...
thanks for the comments from both of you. As well as from Joe S. thru separate email.
0 Likes
Message 5 of 5

Anonymous
Not applicable
Not a problem, glad I could assist.

Joe ...

"hutch" wrote in message news:5742109@discussion.autodesk.com...
'It is most likely, the you added the same instance of your class object
into the two collections.'

Yes... that is what happened.

What I learned today...
Instantiate a class object and add it to two collections.
... editing an object fetched from one collection will effect the object in
the second collection.

I wanted to initially have two collections that were identical.
One used for reference purposes only and the other for editing. Comparison
with the object when edited to the object in the reference collection would
alert my app that the properties changed from their initial values.

Previously, I had created properties in the class module that would indicate
that a value change. This was working well except it couldn't see if it
changed back to the original value.
The only way I could see that I could have this functionality is if I
initially create two collections; a reference collection and a working
collection. This now seems to be working as I wanted...
thanks for the comments from both of you. As well as from Joe S. thru
separate email.
0 Likes