473,239 Members | 1,736 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,239 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 12411
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.