Link to home
Start Free TrialLog in
Avatar of panJacek
panJacek

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of panJacek
panJacek

ASKER

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
 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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/

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial