Hi there quick question
If I have an object (like a custom class I make) and I do the following command
[code]dim myObject1 as new myClass
dim myObject2 as new myClass
Object1 = Object2[/code]
Does this mean that Object1 has actually made a ‘copy’ of Object2? or is Object1 really just referencing the values in Object2?.
This is important because I need to know if I’m making a true copy and I remember in other programming languages this will reference the object. Which means if I edit Object2, I am technically editing Object1 also (which I don’t want). The documentation is not very clear on this. It says it references for comparison but doesn’t say anything about assignment.
If it’s just using a reference how do I make a copy? Would I have to literally take the objects properties 1 by 1 and copy them in?
References, and yes you need to create a new instance and copy the properties.
Object1 = Object2 will point Object1 to the class instance that is pointed to by Object2; so it is a reference. If you want to do a copy instead you will need to create a copy method in the myClass class that copies each property in turn from one object instance to the other.
Ah okay, thanks that is what I wanted to know, I’ll start work on a copy method right now.
Also, if the object referenced by Object1 is has no other reference elsewhere ( e.g. a variable or a property in a module or in another class ) it’s destroyed and no longer exists after
Objetc1=Object2 is executed.
good to know. I think it would be a really useful method to be able to quickly copy everything in a class. This could be done with introspection, makes me wonder if someone has made that? It might not be as efficient to go through introspection perhaps as just manually doing it. Just hurts when you have to copy very complex 3D class objects
Joe, if you are using the classes from the X3 core library (as per our other discussion) for your 3D, have alook at their Clone method.
If you sub-classed the core objects and added new properties, then you’ll have revise the Clone methods to include all the new properties, but the existing Clone methods should save you some time.