473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deep Copy

I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve

Aug 21 '06 #1
4 1841
I assume that you have some way of binding to the original object, i.e. when
the screen cups up it must read data from the object and display it just
redo this with the copy? (if not this might be your problem:))
"Steve" <sf****@ptd.netwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve

Aug 22 '06 #2
Steve wrote:
I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve
If you are looking for a simple Undo mechanism you should look at the
Memento pattern:
http://en.wikipedia.org/wiki/Memento_pattern
http://www.dofactory.com/Patterns/PatternMemento.aspx

You can typically implement that very fast using serialization.

HTH,
Andy

Aug 22 '06 #3
Steve wrote:
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.
Where do you want to commit the changes? To a database? If that is
the case, you can just roll back the transaction if the user clicks the
Cancel button.

Aug 22 '06 #4

Yes, look into the Memento Design Pattern.
http://www.dofactory.com/Patterns/PatternMemento.aspx

If your objects are serializable, then you can try this:


public class CloneHelpers
{
public static object DeepClone(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return b.Deserialize(m);

}
public static bool DeepEquals(object objA,object objB)
{
MemoryStream serA = serializedStream(objA);
MemoryStream serB = serializedStream(objB);
if(serA.Length!=serA.Length)
return false;
while(serA.Position<serA.Length)
{
if(serA.ReadByte()!=serB.ReadByte())
return false;
}
return true;

}
public static MemoryStream serializedStream(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;

return m;
}
}

"Steve" <sf****@ptd.netwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve

Aug 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
4
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I...
6
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.