473,402 Members | 2,046 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,402 software developers and data experts.

Shallow vs Deep copy

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 then dimension another variable
(var1save) as "mystruct" and set var1save = var1 with the intent of changing
var1 data elements and subsequently reasigning var1 back to it's original
values of var1save.

VB.Net makes only a shallow copy of var1 in var1save for anything other than
a simple varable type, i.e., only references for any arrays in the structure
are copied to var1save. Thus, everytime I change a var1 array variable,
var1save also reflects the new value thus negating the intent of saving var1
for future reinstatment.

Currently I have to copy each sub-element of var1 into var1save individually
in order to make a deep copy.

Is there any way to do a deep copy on objects other than breaking them down
into their simple elements and copying each one? This is very annoying to
have to write several lines of code to just copy one variable object to
another of the same type..
--
Dennis in Houston
Nov 21 '05 #1
4 12426
I think you're gonna have to write some code

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
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 then dimension another variable
(var1save) as "mystruct" and set var1save = var1 with the intent of changing var1 data elements and subsequently reasigning var1 back to it's original
values of var1save.

VB.Net makes only a shallow copy of var1 in var1save for anything other than a simple varable type, i.e., only references for any arrays in the structure are copied to var1save. Thus, everytime I change a var1 array variable,
var1save also reflects the new value thus negating the intent of saving var1 for future reinstatment.

Currently I have to copy each sub-element of var1 into var1save individually in order to make a deep copy.

Is there any way to do a deep copy on objects other than breaking them down into their simple elements and copying each one? This is very annoying to
have to write several lines of code to just copy one variable object to
another of the same type..
--
Dennis in Houston

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004
Nov 21 '05 #2
Thanks for reply..that's the same conclusion I've come to. It would be nice
if Microsoft would include a .copyToByVal method with the 2005 issue.

"Hal Rosser" wrote:
I think you're gonna have to write some code

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
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 then dimension another variable
(var1save) as "mystruct" and set var1save = var1 with the intent of

changing
var1 data elements and subsequently reasigning var1 back to it's original
values of var1save.

VB.Net makes only a shallow copy of var1 in var1save for anything other

than
a simple varable type, i.e., only references for any arrays in the

structure
are copied to var1save. Thus, everytime I change a var1 array variable,
var1save also reflects the new value thus negating the intent of saving

var1
for future reinstatment.

Currently I have to copy each sub-element of var1 into var1save

individually
in order to make a deep copy.

Is there any way to do a deep copy on objects other than breaking them

down
into their simple elements and copying each one? This is very annoying to
have to write several lines of code to just copy one variable object to
another of the same type..
--
Dennis in Houston

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004

Nov 21 '05 #3
Dennis,
If you need a Deep Copy I would implement IClonable interface on your
structure, where the implemented Clone method does the actually deep copy.

Then when you needed to Deep Copy I would call the Clone method. You can
implement Clone such that it is type safe.

Something like:

Public Structure MyStruct
Implements ICloneable

Private bytes() As Byte

Public Function Clone() As MyStruct
Dim newMyStruct As MyStruct
newMyStruct.bytes = DirectCast(bytes.Clone, Byte())
Return newMyStruct
End Function

Private Function ICloneable_Clone() As Object Implements
ICloneable.Clone
Return Me.Clone
End Function

End Structure

Dim var1 As MyStruct
Dim var1save As MyStruct
va1save = var1.Clone()

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
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 then dimension another variable
(var1save) as "mystruct" and set var1save = var1 with the intent of changing var1 data elements and subsequently reasigning var1 back to it's original
values of var1save.

VB.Net makes only a shallow copy of var1 in var1save for anything other than a simple varable type, i.e., only references for any arrays in the structure are copied to var1save. Thus, everytime I change a var1 array variable,
var1save also reflects the new value thus negating the intent of saving var1 for future reinstatment.

Currently I have to copy each sub-element of var1 into var1save individually in order to make a deep copy.

Is there any way to do a deep copy on objects other than breaking them down into their simple elements and copying each one? This is very annoying to
have to write several lines of code to just copy one variable object to
another of the same type..
--
Dennis in Houston

Nov 21 '05 #4
Thanks. That will work. I didn't realize you could use the implementation
for structures. I still wish that M'sofe would include a .copytoByVal method
for their objects. Thanks again.

"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis,
If you need a Deep Copy I would implement IClonable interface on your
structure, where the implemented Clone method does the actually deep copy.

Then when you needed to Deep Copy I would call the Clone method. You can
implement Clone such that it is type safe.

Something like:

Public Structure MyStruct
Implements ICloneable

Private bytes() As Byte

Public Function Clone() As MyStruct
Dim newMyStruct As MyStruct
newMyStruct.bytes = DirectCast(bytes.Clone, Byte())
Return newMyStruct
End Function

Private Function ICloneable_Clone() As Object Implements
ICloneable.Clone
Return Me.Clone
End Function

End Structure

Dim var1 As MyStruct
Dim var1save As MyStruct
va1save = var1.Clone()

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
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 then dimension another variable
(var1save) as "mystruct" and set var1save = var1 with the intent of

changing
var1 data elements and subsequently reasigning var1 back to it's original
values of var1save.

VB.Net makes only a shallow copy of var1 in var1save for anything other

than
a simple varable type, i.e., only references for any arrays in the

structure
are copied to var1save. Thus, everytime I change a var1 array variable,
var1save also reflects the new value thus negating the intent of saving

var1
for future reinstatment.

Currently I have to copy each sub-element of var1 into var1save

individually
in order to make a deep copy.

Is there any way to do a deep copy on objects other than breaking them

down
into their simple elements and copying each one? This is very annoying to
have to write several lines of code to just copy one variable object to
another of the same type..
--
Dennis in Houston


Nov 21 '05 #5

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

Similar topics

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 >>>
1
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
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)...
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: Lubomir | last post by:
Hi, I would like to ask if the constructor ArrayList(ICollection c) performs the deep or shallow copy of "c" collection. If it perfoms just shallow copy, is there any method for doing a deep...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
22
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
3
by: saby | last post by:
I would be thankful if any one tell whats the difference between DEEP Copy and Shallow copy. A copy constructor does what type of copy? saby
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.