Clone () is for creating a new object, but what if the object is already created?

Started by
1 comment, last by Roof Top Pew Wee 20 years ago
Still a little new to C#, so forgive me if this is a dumb question. I have an object Camera which I would like to copy to another camera. I''m just wondering if there is a specific way to do a by value copy from one camera to another, like camera1 = camera2; would do in C++, rather than a reference copy. Looked into the Clone() method, but my understanding is that is used when creating a new object. Would the best method be to write a method like: public void SetValuesTo(Camera cameraToSet) { // set all of the variables of cameraToSet to this.variables } Just curious if there is a standard way of doing this in C#. --Vic-- The future of 2D game development: Flat Red Ball
Advertisement
camera2 = camera1.Clone();

Maybe I am just groggy but it seems like that is what you want. The ''old'' values of camera2 are discarded like they would be in a C++ deep-copy.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
That''s just the thing. In your example, antareus, camera2 would be sent to garbage collection and that basically sets camera2 to a new one.

For exmaple, say I have the following:
Camera camera1, camera2, camera3;camera1 = new Camera();camera2 = new Camera();camera3 = camera1;  // so changes in camera 1 are reflected in camera 3camera1.x = 1;camera2.x = 2;// camera3.x should be 1camera1 = camera2.Clone();// at this point, if I understand things correctly, camera1.x will be 2, but camera3.x will still be 1 instead of 2, like it should be.


So you can see, I don''t want to trash the old camera. Just override the values without getting rid of that memory, so that my references aren''t broken.


--Vic--

The future of 2D game development:
Flat Red Ball

This topic is closed to new replies.

Advertisement