Connecting Tech Pros Worldwide Forums | Help | Site Map

Shallow vs Deep copy

Dennis
Guest
 
Posts: n/a
#1: Nov 21 '05
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

Hal Rosser
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Shallow vs Deep copy


I think you're gonna have to write some code

"Dennis" <Dennis@discussions.microsoft.com> wrote in message
news:089CD328-4700-4CEB-AA76-46AE4A63EDE0@microsoft.com...[color=blue]
> I have several Data Structures, say "mystruct" which contain arrays of[/color]
bytes,[color=blue]
> other structures, etc. I then dimension a variable (var1) as "mystruct"[/color]
and[color=blue]
> set the various elements var1 to data. I then dimension another variable
> (var1save) as "mystruct" and set var1save = var1 with the intent of[/color]
changing[color=blue]
> 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[/color]
than[color=blue]
> a simple varable type, i.e., only references for any arrays in the[/color]
structure[color=blue]
> are copied to var1save. Thus, everytime I change a var1 array variable,
> var1save also reflects the new value thus negating the intent of saving[/color]
var1[color=blue]
> for future reinstatment.
>
> Currently I have to copy each sub-element of var1 into var1save[/color]
individually[color=blue]
> in order to make a deep copy.
>
> Is there any way to do a deep copy on objects other than breaking them[/color]
down[color=blue]
> 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[/color]


---
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


Dennis
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Shallow vs Deep copy


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:
[color=blue]
> I think you're gonna have to write some code
>
> "Dennis" <Dennis@discussions.microsoft.com> wrote in message
> news:089CD328-4700-4CEB-AA76-46AE4A63EDE0@microsoft.com...[color=green]
> > I have several Data Structures, say "mystruct" which contain arrays of[/color]
> bytes,[color=green]
> > other structures, etc. I then dimension a variable (var1) as "mystruct"[/color]
> and[color=green]
> > set the various elements var1 to data. I then dimension another variable
> > (var1save) as "mystruct" and set var1save = var1 with the intent of[/color]
> changing[color=green]
> > 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[/color]
> than[color=green]
> > a simple varable type, i.e., only references for any arrays in the[/color]
> structure[color=green]
> > are copied to var1save. Thus, everytime I change a var1 array variable,
> > var1save also reflects the new value thus negating the intent of saving[/color]
> var1[color=green]
> > for future reinstatment.
> >
> > Currently I have to copy each sub-element of var1 into var1save[/color]
> individually[color=green]
> > in order to make a deep copy.
> >
> > Is there any way to do a deep copy on objects other than breaking them[/color]
> down[color=green]
> > 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[/color]
>
>
> ---
> 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
>
>
>[/color]
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Shallow vs Deep copy


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" <Dennis@discussions.microsoft.com> wrote in message
news:089CD328-4700-4CEB-AA76-46AE4A63EDE0@microsoft.com...[color=blue]
> I have several Data Structures, say "mystruct" which contain arrays of[/color]
bytes,[color=blue]
> other structures, etc. I then dimension a variable (var1) as "mystruct"[/color]
and[color=blue]
> set the various elements var1 to data. I then dimension another variable
> (var1save) as "mystruct" and set var1save = var1 with the intent of[/color]
changing[color=blue]
> 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[/color]
than[color=blue]
> a simple varable type, i.e., only references for any arrays in the[/color]
structure[color=blue]
> are copied to var1save. Thus, everytime I change a var1 array variable,
> var1save also reflects the new value thus negating the intent of saving[/color]
var1[color=blue]
> for future reinstatment.
>
> Currently I have to copy each sub-element of var1 into var1save[/color]
individually[color=blue]
> in order to make a deep copy.
>
> Is there any way to do a deep copy on objects other than breaking them[/color]
down[color=blue]
> 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[/color]


Dennis
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Shallow vs Deep copy


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:
[color=blue]
> 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" <Dennis@discussions.microsoft.com> wrote in message
> news:089CD328-4700-4CEB-AA76-46AE4A63EDE0@microsoft.com...[color=green]
> > I have several Data Structures, say "mystruct" which contain arrays of[/color]
> bytes,[color=green]
> > other structures, etc. I then dimension a variable (var1) as "mystruct"[/color]
> and[color=green]
> > set the various elements var1 to data. I then dimension another variable
> > (var1save) as "mystruct" and set var1save = var1 with the intent of[/color]
> changing[color=green]
> > 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[/color]
> than[color=green]
> > a simple varable type, i.e., only references for any arrays in the[/color]
> structure[color=green]
> > are copied to var1save. Thus, everytime I change a var1 array variable,
> > var1save also reflects the new value thus negating the intent of saving[/color]
> var1[color=green]
> > for future reinstatment.
> >
> > Currently I have to copy each sub-element of var1 into var1save[/color]
> individually[color=green]
> > in order to make a deep copy.
> >
> > Is there any way to do a deep copy on objects other than breaking them[/color]
> down[color=green]
> > 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[/color]
>
>
>[/color]
Closed Thread