473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I copy a collection class from one instance to another?

Either I don't understand (entirely possible), or there's no way to
copy parts of a class hierarchy from one instance to another.

Say I have a class called Foo, and it contains, among other things, a
custom collection called SubFoos. If I do something like

Dim MyFoo1 As Foo
Dim MyFoo2 As Foo

'call something to populate MyFoo1 and its SubFoos...

Debug.Print MyFoo1.SubFoos. Count

'pretend it's, say, 18. What a lot of SubFoos!

Set MyFoo2 = MyFoo1

Set MyFoo1 = Nothing

Debug.Print MyFoo2.SubFoos. Count

The last line will always give me 0. The only way, it seems, to copy
the members of the SubFoos collection from MyFoo1 to MyFoo2 is to
iterate through it, and add each one. And yet all the Foo-level stuff
is there.

I think this has something to do with pointers and other such "real"
"programmin g" notions that I have never learned, but still, frankly, it
blows. What's the point of building all that sexy self-enforcing
structure only to have to turn around and walk it like a plain old
recordset or whatever when you want a simple copy? Is this an
Access/VBA problem, or is it common to all programming languages? Or
am I just too green to get it?

Apr 14 '06 #1
8 9633

You don't give the implementation details or your collection class but I'm
guessing your doing something like exposing the collection as a public
member of the class. If so this is wrong, the collection should be private
to the class and you then provide methods of the class to manipulate the
collection.

The following is a very simple example of a collection class implementation
(I've omitted the Item and Remove methods for clarity).
Foo Class
=========
Option Explicit

Private SubFoos As Collection

Sub Add(RHS As Object)
If SubFoos Is Nothing Then
Set SubFoos = New Collection
End If
SubFoos.Add RHS
End Sub

Function Count() As Long
Count = SubFoos.Count
End Function

=========

Test Code
=========
Function TestFoo()
Dim MyFoo1 As Foo
Dim MyFoo2 As Foo
Dim MyObj As Object
Dim intX As Integer

Set MyFoo1 = New Foo

For intX = 1 To 10
MyFoo1.Add MyObj
Next
Debug.Print MyFoo1.Count

Set MyFoo2 = MyFoo1
Debug.Print MyFoo1.Count, MyFoo2.Count

Set MyFoo1 = Nothing
Debug.Print MyFoo2.Count
Set MyFoo2 = Nothing

End Function

=========

Results
=========
10
10 10
10

=========
--

Terry Kreft
"downwitch" <do*******@gmai l.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
Either I don't understand (entirely possible), or there's no way to
copy parts of a class hierarchy from one instance to another.

Say I have a class called Foo, and it contains, among other things, a
custom collection called SubFoos. If I do something like

Dim MyFoo1 As Foo
Dim MyFoo2 As Foo

'call something to populate MyFoo1 and its SubFoos...

Debug.Print MyFoo1.SubFoos. Count

'pretend it's, say, 18. What a lot of SubFoos!

Set MyFoo2 = MyFoo1

Set MyFoo1 = Nothing

Debug.Print MyFoo2.SubFoos. Count

The last line will always give me 0. The only way, it seems, to copy
the members of the SubFoos collection from MyFoo1 to MyFoo2 is to
iterate through it, and add each one. And yet all the Foo-level stuff
is there.

I think this has something to do with pointers and other such "real"
"programmin g" notions that I have never learned, but still, frankly, it
blows. What's the point of building all that sexy self-enforcing
structure only to have to turn around and walk it like a plain old
recordset or whatever when you want a simple copy? Is this an
Access/VBA problem, or is it common to all programming languages? Or
am I just too green to get it?

Apr 14 '06 #2
"downwitch" <do*******@gmai l.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
Either I don't understand (entirely possible), or there's no way to
copy parts of a class hierarchy from one instance to another.

Set MyFoo2 = MyFoo1


Unfortunately, there is no object clone method in access...

You can prevent the object from going out of scope like

So, in the above...you just are pointing MyFoo2 to MyFoo1...

However!!..if you create a new instance of the object FIRST and then set it
to the previous ..you get a copy of the pointer..but NOT the data!!

So, change the above to

set MyFoo2 = new Foo

set MyFoo2 = MyFoo1
set MyFoo1 = nothing....

At this point, you CAN now still use MyFoo2. However, this will NOT SOLVE
your problem. They are still both the SAME object...if you change one..the
other changes. (but, now we only got one since the use of the =
nothing...but they are NOT independent).

Unfortunately, this means you have to write your own clone method of the
class....

you can use

public function myClone as Foo

dim f as new foo

f.m_BollingPoin t = me.m_BollingPoi nt
......et.c etc. etc...

You have to write one line of code to for everything and peiice of data your
have....

set MyClone = f

end function.

Then, you can go

dim antoherFoo as new foo

set anotherFoo = myFoo1.MyClone

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Apr 14 '06 #3
In place of the = nothing...try changing the value of one of the
objects....you will see that the other changes....

eg:

Set MyFoo2 = MyFoo1
Debug.Print MyFoo1.Count, MyFoo2.Count

MyFoo1.Add MyObj <--- add this

Set MyFoo1 = Nothing

Debug.Print MyFoo2.Count
Set MyFoo2 = Nothing

you will get a value of 11 for the above last print....despit e the first
object being set = nothing...

When you set MyFoo1 = nothing...you are killing the pointer...but they both
are still the same copy.....

Try adding one value to the first one BEFORE you do a set MyFoo1 =
nothing...and you will STILL find the 2nd instance will have that new
value...

You have to write code to copy the class......

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Apr 14 '06 #4
rkc
downwitch wrote:
Either I don't understand (entirely possible), or there's no way to
copy parts of a class hierarchy from one instance to another.


You should really post actual code that you have tried
for something like this. I don't see the behaviour you
describe so it's must be your implementation.

<clsStuff>
Option Compare Database
Option Explicit

Private stuff As VBA.Collection
Private index As Long

Public Function NextStuff() As Variant
index = index + 1
If index <= stuff.count Then
NextStuff = stuff(index)
End If
End Function

Public Function HasMoreStuff() As Boolean
HasMoreStuff = index < stuff.count
End Function

Public Sub ResetStuff()
index = 0
End Sub

Public Sub AddStuff(v As Variant)
stuff.Add v
End Sub

Private Sub Class_Initializ e()
Set stuff = New VBA.Collection
End Sub

Private Sub Class_Terminate ()
Set stuff = Nothing
End Sub
</clsStuff>

<Test Form>
Option Compare Database
Option Explicit

Dim stf2 As clsStuff

Sub TestStuff()
Dim stf As clsStuff

Set stf = New clsStuff
Set stf2 = New clsStuff

With stf
.AddStuff "Thing One"
.AddStuff "Thing Two"
.AddStuff "Thing Three"
While .HasMoreStuff
Debug.Print .NextStuff
Wend
Set stf2 = stf
Set stf = Nothing
End With

Set stf = Nothing

End Sub

Sub ShowStuff2(stf As clsStuff)
stf.ResetStuff
While stf.HasMoreStuf f
Debug.Print "2: "; stf.NextStuff
Wend
End Sub

Private Sub Command0_Click( )
Call TestStuff
End Sub

Private Sub Command1_Click( )
Call ShowStuff2(stf2 )
End Sub

</Test Form>
Apr 14 '06 #5
This is the point. The other posts don't show the key thing here,
which is that even though you can set an Instance2 to _point to_ an
Instance1, it's not because you say "make Instance1 nothing" that it
truly goes away. In other words, it's not a clone, it's two pointers
to the same single object.

I would've posted real code, but (1) my class hierarchies are
hydra-like, huge, and impossible, and (2) I am more interested in the
theoretical wrongheadedness of why Access/VBA doesn't do this properly,
and (less importantly) if this is common to classes in other
programming environments. Besides, Albert nailed it with his simple
addition to Terry's code: change one, you change the other, meaning
there's one, not two. Still a slightly different question from why
public instances of the same class collection can't be set to equal
each other, but the two phenomena must be linked; if you could make a
proper clone of an instance, then you could reasonably expect to make a
proper clone of a part of an instance.

Is this different from working with, say, two string variables, or is
it all just pointers? At what point does something really get
"created"?
Albert D. Kallal wrote:
In place of the = nothing...try changing the value of one of the
objects....you will see that the other changes....

eg:

Set MyFoo2 = MyFoo1
Debug.Print MyFoo1.Count, MyFoo2.Count

MyFoo1.Add MyObj <--- add this

Set MyFoo1 = Nothing

Debug.Print MyFoo2.Count
Set MyFoo2 = Nothing

you will get a value of 11 for the above last print....despit e the first
object being set = nothing...

When you set MyFoo1 = nothing...you are killing the pointer...but they both
are still the same copy.....

Try adding one value to the first one BEFORE you do a set MyFoo1 =
nothing...and you will STILL find the 2nd instance will have that new
value...

You have to write code to copy the class......

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal


Apr 15 '06 #6
rkc
downwitch wrote:
This is the point. The other posts don't show the key thing here,
which is that even though you can set an Instance2 to _point to_ an
Instance1, it's not because you say "make Instance1 nothing" that it
truly goes away. In other words, it's not a clone, it's two pointers
to the same single object.

I would've posted real code, but (1) my class hierarchies are
hydra-like, huge, and impossible, and (2) I am more interested in the
theoretical wrongheadedness of why Access/VBA doesn't do this properly,
and (less importantly) if this is common to classes in other
programming environments. Besides, Albert nailed it with his simple
addition to Terry's code: change one, you change the other, meaning
there's one, not two. Still a slightly different question from why
public instances of the same class collection can't be set to equal
each other, but the two phenomena must be linked; if you could make a
proper clone of an instance, then you could reasonably expect to make a
proper clone of a part of an instance.

Is this different from working with, say, two string variables, or is
it all just pointers? At what point does something really get
"created"?


I did completely miss your actual question.
In Visual Basic/VBA there is no built into the language way
of creating an independent clone of an object. You have to
do what you don't think you should have to do. That is to copy
the actual base data piece by piece.

You can ponder it till the end of time, but that's the way it is.

Apr 15 '06 #7
>if this is common to classes in other
programming environments.

Yes, it is common to true object programming environments. You often have to
still write a clone method, but the properties and methods of the object
*usually* can be un-corked out to a serialized string that can be saved (in
fact, we call this serialization). It is this "string" of data that can
then be loaded back into the object.

This gives two benefits.

you can actually "save" the state of the object to your hard disk..and
next time you run the program...re-load the object. Thus, you can actually
use objects, and save them to a database, or even a file on hard disk.

The side benefit of this serialization process is that the code to clone
a object his thus very easy to write....

And, even more important, with web based services...you can *transfer* a
object across the connection this way.

So, if the language at hand does not support clone of the object (not all
do), but does support what we call serialization, then cloning is easy
anyway...

We have neither clone, nor serialization available in VB, or ms-access,
which by the way is the same as VB6...

You do have serialization in vb.net however.....
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Apr 18 '06 #8
OK, I thought it was fairly obvious what was on from my example.

A class variable contains a pointer to a piece of memory, initially this is
null but when you instantiate it using the New keyword (or CreateObject)
then a piece of memory is set aside for the class an the variable is set to
point to this piece of memory.

If you have a second variable which you set equal to an existing class
variable then you are copying the value of that variable, that is you are
copying the pointer to the memory which holds the class information. So you
aren't creating a copy of the class you are creating a copy of the pointer
to the class.

In order to create a copy of the class you would need to do one of two
things.
a)
instantiate a new class object
copy all the property values from the original to the new class
object

This isn't so difficult if you implement a properties collection on the
class

You could in theory
b)
instantiate a new class object
determine the length of memory containing the class data of the
original
copy the memory block of the original class data over the new class
data

This would involve a fair amount of API work.

--

Terry Kreft
"downwitch" <do*******@gmai l.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
This is the point. The other posts don't show the key thing here,
which is that even though you can set an Instance2 to _point to_ an
Instance1, it's not because you say "make Instance1 nothing" that it
truly goes away. In other words, it's not a clone, it's two pointers
to the same single object.

I would've posted real code, but (1) my class hierarchies are
hydra-like, huge, and impossible, and (2) I am more interested in the
theoretical wrongheadedness of why Access/VBA doesn't do this properly,
and (less importantly) if this is common to classes in other
programming environments. Besides, Albert nailed it with his simple
addition to Terry's code: change one, you change the other, meaning
there's one, not two. Still a slightly different question from why
public instances of the same class collection can't be set to equal
each other, but the two phenomena must be linked; if you could make a
proper clone of an instance, then you could reasonably expect to make a
proper clone of a part of an instance.

Is this different from working with, say, two string variables, or is
it all just pointers? At what point does something really get
"created"?
Albert D. Kallal wrote:
In place of the = nothing...try changing the value of one of the
objects....you will see that the other changes....

eg:

Set MyFoo2 = MyFoo1
Debug.Print MyFoo1.Count, MyFoo2.Count

MyFoo1.Add MyObj <--- add this

Set MyFoo1 = Nothing

Debug.Print MyFoo2.Count
Set MyFoo2 = Nothing

you will get a value of 11 for the above last print....despit e the first
object being set = nothing...

When you set MyFoo1 = nothing...you are killing the pointer...but they both are still the same copy.....

Try adding one value to the first one BEFORE you do a set MyFoo1 =
nothing...and you will STILL find the 2nd instance will have that new
value...

You have to write code to copy the class......

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal

Apr 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1708
by: Mark Buxbaum | last post by:
Hi, Is is possible to use a collection class instance as a helper object in a class? For example: MyClass.h: ---------- class MyClass {
7
7901
by: Steven.Xu | last post by:
Hello everyone! I have a problem about create an instance automatically in C#. Now I have a class: namespace1.namespace11.CClass01. I want to create it's instance in a function named CreateObjInstance. And the function not only generate just CClass01. So I must sent the class type(whit it's namespace) into the function. There has two question following. The first is I can't send a class type into function. It must is a object instance....
6
1394
by: Gerard Flanagan | last post by:
Hello If I have the Vector class below, is there a means by which I can have the following behaviour >>>A = Vector(1, 2) >>>print A (1, 2) >>>A = 0
19
4921
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
3
6249
by: Chris Pratt | last post by:
I'm sorry to ask such a fundamental question, but is it possible to create a copy of an object in VB.Net 2005? I have an app which, on load, creates an instance of a class, into which it reads a load of data from XML. What I want is to be able to declare a new instance of this class and make it a copy of the existing instance. This way I can spawn various instances with their own updated properties The code "Dim NewInst as MyClass =...
2
2131
by: TEK | last post by:
Hello We're having a issue with a part of our app that is very strange, and I'm unable to see that it should be possible. We have: A collection (ActivityCollection) that is inherited from a general, abstract collection class that implements ITypedList and IBindingList When the program execute, the ITypedList.GetItemProperties method is
5
8256
by: DBC User | last post by:
I have 2 classes which are exactly same except the name. I do not have access to either of the class to change anything in it. In my code, I need to copy one of one class to another class. How can I do this? class A { .... } class B
5
2166
by: Joseph Geretz | last post by:
Of course, I can store a C# class instance to the Server Cache (this.Context.Cache). I've tried it. My question is, will this destroy the scalability of my application? My background is VB6. With VB6 we learned very early on that you do *NOT* store VB6 class instances on the server. That is because a VB6 class can only be accessed on the thread on which it is created (thread affinity). This is just incompatible with stateless web...
6
15402
by: ffreino | last post by:
Hi, I have a Solution with two different Projects. In one project, can I import a class from another project? It's something like: Solution mySolution ProjectA Class A1
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.