Copying values of fields of class not it's address.

Hello experts :)

Example:

class Class1
{
public int field;
}

Class1 class1a = new Class();
Class1 class1b = new Class();

class1b = class1a;

class1b.field = 12;

Problem:
The problem is, that it also changes the value of class1a.field

Question:
How can I copy values from class1a to class1b not it's memory address.

thank you

panJacek
panJacekAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
anarki_jimbelConnect With a Mentor Commented:
What you show above is pure OO.

When you write

Class1 class1a = new Class();
Class1 class1b = new Class();

you declare and create TWO objects of the class1. class1a and class1b point to different objects

Then, you write:

class1b = class1a;

Now, "pointer" class1b became same as class1a. I.e they pont to the same address, indeed. I.e. - to the SAME object. Really, the object you created initially writing Class1 class1b = new Class(); is lost (nothing points to it - it will be garbage collected)!

Therefore, now does not matter what name you use, class1a or class1b, they point to the same object.

So, if you remove third line

Class1 class1a = new Class();
Class1 class1b = new Class();

class1b.field = 12;

class1a object won't be affected.

If you need to copy something from 1b to 1a , you write:

class1b.field = 12;
class1a.field = class1b.field;
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Done manually:

    Class1 class1a = new Class();
    Class1 class1b = new Class();

    class1a.field = 12;

    class1b.field = class1a.field; // copy value from "a" to "b"

Generically speaking, you want a Clone() method...but you need to decide whether this should create a NEW instance of Class1 or allow you to overwrite the values in an existing instance.
0
 
panJacekAuthor Commented:
I need to copy all values from class1a to class1b, then make some changes to class1b keeping class1a intact.

I will use class1a for later use and this is why I want to leave it intact.

How can I do it? I do not really want to manually assign each field value from class1a to class1b.

panJacek
 
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.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use a MemoryStream and the BinaryFormatter to make your own Clone() method:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 class1a = new Class1();
            class1a.field = 8;
            Console.WriteLine("a: " + class1a.field.ToString());
            Console.WriteLine("After Clone:");
            Class1 class1b = (Class1)class1a.Clone();
            Console.WriteLine("b: " + class1b.field.ToString());

            class1b.field = 12;
            Console.WriteLine("After change:");
            Console.WriteLine("a: " + class1a.field.ToString());
            Console.WriteLine("b: " + class1b.field.ToString());
        }
    }

    [Serializable()]
    class Class1
    {
        public int field;

        public Class1 Clone()
        {
            BinaryFormatter BF = new BinaryFormatter();
            MemoryStream MS = new MemoryStream();
            BF.Serialize(MS, this);
            MS.Flush();
            MS.Position = 0;
            return (Class1)BF.Deserialize(MS);
        }

    }

}

Open in new window

0
 
anarki_jimbelCommented:
I believe the best way to use Reflection for that. There is a beast called "Copy Constructor" - have a look at the implementation - simple and efficient:

http://www.johnsadventures.com/archives/2006/07/an_intelligent_copy_constructor_in_c_usi/

0
 
andrewjbConnect With a Mentor Commented:
...or us 'struct' rather than 'class'.

But it rather depends on what the actual problem is.
0
All Courses

From novice to tech pro — start learning today.