• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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
0
CyrexCore2k
Asked:
CyrexCore2k
  • 4
  • 4
  • 3
1 Solution
 
Richie_SimonettiIT OperationsCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
and doing

MsgBox myCollection.Item(2).Name
you will see that object 1 hasn't changed but object2 does so.
0
 
CyrexCore2kAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DiveblueCommented:
create a procedure in the object...

Public Function ObjectCopy() as testObj

set oMyObj = new testObj
myobj.property1 = me.property1
myobj.property2 = me.property2

set ObjectCopy = oMyObj
set oMyObj = nothing
end function

then .....
- 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.ObjectCopy
myObject.Name = "The object was referenced..."

MsgBox myCollection.Item(1).Name
End Sub
- End Code -
0
 
CyrexCore2kAuthor Commented:
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
0
 
Richie_SimonettiIT OperationsCommented:
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)
0
 
DiveblueCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
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
?
0
 
CyrexCore2kAuthor Commented:
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
0
 
DiveblueCommented:
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
0
 
CyrexCore2kAuthor Commented:
http://www.mvps.org/vb/hardcore/html/comraqs.htm
Aight I guess this makes sense then >.<

Points go to Diveblue Ty everyone

Spencer Ruport
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now