?
Solved

Spawning new objects???

Posted on 2003-03-11
11
Medium Priority
?
203 Views
Last Modified: 2013-11-23
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
Comment
Question by:CyrexCore2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8114566
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8114574
and doing

MsgBox myCollection.Item(2).Name
you will see that object 1 hasn't changed but object2 does so.
0
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 8115018
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
Diveblue earned 800 total points
ID: 8115278
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
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 8115318
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119191
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
 
LVL 3

Expert Comment

by:Diveblue
ID: 8119833
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119912
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
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 8120678
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
 
LVL 3

Expert Comment

by:Diveblue
ID: 8120762
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
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 8120816
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question