Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

making a new object equal to an old object but not a reference...

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I have a class, call it myClass and it consists of a number of properties and methods. I also have a collection that I am iterating through that contains a number of myClass objects.


private best as myClass 'this is used to hold the best value found

private myCollection as collection

private sub main()
dim temp as myClass
dim i as long

'There are a number of steps here that are not important but they lead to the initialization of myCollection with myClass objects


for i = 1 to 100

for each temp in myCollection
if temp.value < best.value then
set best = temp
end if
next temp

'Another sub here goes through and re-evaluates the collection

next i

'print out the properties of best here

end sub


The above example is simplistic but gets the point across. I know what is happening, each time the statement:

set best = temp

is executed, the best is pointed to the object that temp is pointed to. What I need to happen is that a new object is created (that is identical to the object pointed to by temp but in a different location in memory) and the memory location is stored in best.

I hope that I made myself clear. Any help would be greatly appreciated......

P.S. I am currently solving the problem by basiclly doing this:

set best = new myClass
best.value = temp.value 'and so on setting all the properties....


Troy Williams B.Eng.
fenris@hotmail.com

 
That is exactly what you have to do. You could put a clone function inside your class. You create a new instance of the class inside the current class, copy over the properties to the new object and return the new instance (object) with a set.
Code:
Function Clone() as MyClass
    Dim objClass as New myclass
    ...Copy properties
        objClass.prop1 = mstrProp1 ' don't use me.prop1. it actually calls the property method
        Set Clone = objClass
        Set objClass = Nothing
End Function
 
Thanks for the reply! It is too bad that you can't do something like:

set b = new a

or something like that....

Anyway, I will modify my routines..... Troy Williams B.Eng.
fenris@hotmail.com

 
Hey fenris,

You CAN do that.
Test this out:

Create a form.
Put a command button on it.

Code:
Private Sub Command1_Click()
    Dim frmForm as New Form1
    frmForm.Visible = True
End Sub

You CAN do it exactly like that.
--NipsMG
 
Nips - what are you talking about???

fenris wants to create a copy of an object whilst keeping the properties intact.

fenris, I have a DLL which will do what you want after a fashion - it has some limitations, though. What it does is Serialize an object to XML. You can then use this data to create an instance of your object.

For example:

Dim s as Serialize
Dim t as String
Dim o as object

Set s = new Serialize
t = s.SaveObjectToSerialized (yourObject)
Set o = s.SetObjectFromSerialized (t)

o will then be a replica of yourObject. The limitations are these: It doesn't handle property arrays, it doesn't handle dictionaries, and it can be slow when serializing large hierarchies of objects. Also, the properties that you want included in the stream must have public Get/Let/Set methods.

Chaz
 
Thanks for the input, it is greatly appreciated.

Scorpio66, very interesting idea! Since I am not familiar with XML, I think I'll stick with a &quot;clone&quot; type function, i.e. one that simply assigns the properties to the new object.

It's just too bad there is no easy way. In most of the projects that I have worked on this was never an issue. But for this particular project I am involved with it is a major issue.


Regards Troy Williams B.Eng.
fenris@hotmail.com

 
Cloning is the best way if you have control over your objects and can write the cloning routine. I had to adopt this procedure because I needed to cache the parameters of unknown objects in the database in a scheduling program.

If you have multiple classes that you want to clone then you might want to write an IClone interface and have your classes Implement the interface. That introduces some saftey into your code.

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top