473,799 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12463
I think you're gonna have to write some code

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:08******** *************** ***********@mic rosoft.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****@discuss ions.microsoft. com> wrote in message
news:08******** *************** ***********@mic rosoft.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.byt es = DirectCast(byte s.Clone, Byte())
Return newMyStruct
End Function

Private Function ICloneable_Clon e() As Object Implements
ICloneable.Clon e
Return Me.Clone
End Function

End Structure

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

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:08******** *************** ***********@mic rosoft.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.byt es = DirectCast(byte s.Clone, Byte())
Return newMyStruct
End Function

Private Function ICloneable_Clon e() As Object Implements
ICloneable.Clon e
Return Me.Clone
End Function

End Structure

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

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:08******** *************** ***********@mic rosoft.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
12354
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
1911
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
4
52312
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) 10.What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
5
25652
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 Collection classes does not support deep copy methods by theself, only Clone with shallow copy.
2
2732
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 one? Thanks, Lubomir
26
15815
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
15949
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 = (ArrayList) a.Clone();
3
32702
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
3150
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
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.