C# Copy Object

Hi,

I have an object (please see code block below) which I want to make a copy of. However when I make a copy of my object and make a change to one the other object also changes.

I do not want to use serialisation either. Can someone please send me some code.

Thanks for any help,
M

public class OrderBlock
    {
        public string BlockID { get; set; }
        public int NumFails { get; set; }
        public List<Orders> OrderList { get; set; }
        public List<Orders> OrderListFailed { get; set; }
    }

    public class Orders
    {
        public bool Failed { get; set; }
        public string Name { get; set; }
        public string ID { get; set; }
        public string Side { get; set; }
        public double Amount { get; set; }
        public bool GoodOrder { get; set; }
    } 

Open in new window

mcs26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
Provide an example. What do you change and what other object also gets changed.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>However when I make a copy of my object and make a change to one the other object also changes.

obj o = new obj();// some object based on a class
obj o2 = o;  // this is NOT making a copy, both o2 and o are the same object;

you need
obj o = new obj();
obj o2 = new obj();
//now copy the contents of o into o2 to make a copy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
have you tried the Clone method?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.