?
Solved

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

Posted on 2010-01-11
7
Medium Priority
?
272 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:panJacek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 920 total points
ID: 26288765
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 920 total points
ID: 26288774
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
 

Author Comment

by:panJacek
ID: 26289010
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Assisted Solution

by:zadeveloper
zadeveloper earned 80 total points
ID: 26289149
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26289204
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 26289498
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
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 80 total points
ID: 26292054
...or us 'struct' rather than 'class'.

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question