Link to home
Start Free TrialLog in
Avatar of CyrexCore2k
CyrexCore2kFlag for United States of America

asked on

Spawning new objects???

I'm having trouble figuring out how to spawn a new object. Basically after I add an object into a collection I want the object in the collection to be a completely new object... not a common reference to the object I originally added with... If I'm explaining this poorly here's my code below.

- Start Code -
Private Sub Form_Load()
Dim myCollection As New Collection
Dim myObject As New testObj
myObject.Name = "A new testObj Object was spawned!"
myCollection.Add myObject
myObject.Name = "The object was referenced..."

MsgBox myCollection.Item(1).Name
End Sub
- End Code -

Notice how I change myObject.Name's value but it affects the object stored in the collection. I know this happens because the messagebox says "This object was referenced...". I want to know how to spawn a new object without having to copy each property value... is there some way to do this?

FYI testObj is a class I made just for example

- Start Code -
Private c_Name As String
Public Property Get Name() As String
    Name = c_Name
End Property
Public Property Let Name(ByVal vData As String)
    c_Name = vData
End Property
- End Code -

Tyvm :-D
Spencer Ruport
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

you are adding an object to a collection but not a new one inside it, it is always the same object.
If you want to add "new" objects, just add another one to the collection

Private Sub Form_Load()
Dim myCollection As New Collection
Dim myObject As New testObj
myObject.c_Name = "A new testObj Object was spawned!"
myCollection.Add myObject

Dim myObject2 As New testObj
myObject2.c_Name = "A testObj2 Object was spawned!"
myCollection.Add myObject2
myObject2.Name = "The object was referenced..."

MsgBox myCollection.Item(1).Name
End Sub
and doing

MsgBox myCollection.Item(2).Name
you will see that object 1 hasn't changed but object2 does so.
Avatar of CyrexCore2k

ASKER

Yeah that works but I was hoping there was a better solution for say like 70 entries... and no arrays either. Is there no simple way to just copy an object?

Spencer Ruport
ASKER CERTIFIED SOLUTION
Avatar of Diveblue
Diveblue

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's a little bit closer yes... but is there a way to make that function more general? To work with any sort of object?

Spencer Ruport
I don't like to use that kind of "circular references" with objects but if you need a more general approach use a generic CopyObject fucntion with return value "as object"
(slow)
Avatar of Diveblue
Diveblue

Richie, There are no circular references involved here. A circular reference occurs when 2 objects hold references to each other.

Cyrex
 You could theoretically use the property bag to save and load the object from disk, but that is not generic either.

scott
Diveblue: you are right when you say:
"A circular reference occurs when 2 objects hold references to each other."
What does this
Public Function ObjectCopy() as testObj
?
Simonetti, yeah that's kinda my trouble.

Dim a as Object
Dim b as Object
Set a = New Object
Set b = a

The code above makes a pointer from b to a and a is a pointer to an object. I'm having a hard time understanding why vb didn't just include a simple spawnObject function in it. I guess I might just have to take Diveblue's recommendation and just make each class I create have a method in it to copy the properties one by one...

>.< Doh!


Spencer Ruport
Richie,

The function returns a reference to oMyObj, which as you can see in the code is a newly created object, rather than a reference to the existing one. Regardless, if it did return areference to itself that would be multiple referencing rather than circular referencing.

scott
http://www.mvps.org/vb/hardcore/html/comraqs.htm
Aight I guess this makes sense then >.<

Points go to Diveblue Ty everyone

Spencer Ruport