By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

Shallow vs Deep copy

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.