VB6 conversion of UDT to structure - ValueType?

  • Thread starter Thread starter Chuck Ritzke
  • Start date Start date
C

Chuck Ritzke

Okay, just when I thought I was starting to understand stuff. In VB6...

Type MyDataType
Dim Value1 as double
Dim Value2 as double
End Type

Dim MyData1 as MyDataType
Dim MyData2 as MyDataType

MyData1=MyData2

'...Do stuff to MyData2, MyData1

When I did this in VB6, MyData1 and MyData2 were separate values (ie if I
changed values on one subsequently, the other remained unchanged). When the
code is converted to VB.NET, it changes Type to Structure. I read
documentation and it says structures are ValueTypes and not ReferenceTypes
like classes. So I expected the same behavior as in VB6. But in my converted
code, it appears that MyData1 is just a reference to MyData2 because
subsequent changes to one, change the other. What gives? And what is the
easiest way to create a cloned MyData1 instead of just a reference to
MyData2? I hope I don't have to go thru my VB6 code and set each value
inside the structure.

TIA,
Chuck
 
"Chuck Ritzke" said:
Okay, just when I thought I was starting to understand stuff. In VB6...

Type MyDataType
Dim Value1 as double
Dim Value2 as double
End Type

Dim MyData1 as MyDataType
Dim MyData2 as MyDataType

MyData1=MyData2

'...Do stuff to MyData2, MyData1

When I did this in VB6, MyData1 and MyData2 were separate values (ie if I
changed values on one subsequently, the other remained unchanged). When the
code is converted to VB.NET, it changes Type to Structure. I read
documentation and it says structures are ValueTypes and not ReferenceTypes
like classes. So I expected the same behavior as in VB6. But in my converted
code, it appears that MyData1 is just a reference to MyData2 because
subsequent changes to one, change the other. What gives?

There must be something else wrong in your code. I was unable to
produce such behavior using this code:

Option Strict On
Option Explicit On

Structure MyDataType
Dim val1 As Double
Dim val2 As Double
End Structure

Module Module1

Sub Main()
Dim d1 As MyDataType
Dim d2 As MyDataType

d1.val1 = 11
d1.val2 = 22

d2 = d1

d1.val1 = 999
d1.val2 = 888
End Sub

End Module
 
Thanks Patrick,

I looked further and realized that my problem happens because the value
inside the structure are arrays. Arrays inside the structure become
references to each other whereas primitive variables stay independent. It
sure would help to have a quick fix to handle this difference between VB6
and .NET.

So modifying your example...

Structure MyDataType
Dim val1() As Double
Dim val2 As Double
End Structure

Module Module1

Sub Main()
Dim d1 As MyDataType
Dim d2 As MyDataType
reDim d1.val1(1)
reDim d2.val1(1)

d1.val1(1) = 11
d2.val1(1) = 22
d1.val2=44
d2.val2=55
'AT THIS POINT BOTH ARRAYS AND VARIABLES INSIDE d1 and d2 ARE
INDEPENDENT

d2=d1

d2.val1(1) = 999
d2.val2=888
'NOW THE RESULT IS
'd1.val(1)=999 and d2.val(1)=999 and now reference each other
whereas...
'd1.val2=55 and d2.val2=88 are still independent

End Sub

End Module
 
It looks like I can write a "cloning" function for each UDT to handle
arrays...

Function CloneMyDataType(MyData as MyDataType)

Dim MyCopy as MyDataType

MyCopy=MyData
MyCopy.Val1=VB6.CopyArray(MyData.Val1)
'and repeat last line for each array in MyDataType

Return MyCopy

End Function

'Then instead of d2=d1

d2=CloneMyDataType(d1)


But do you have a quicker/better way? I have lots of UDT's each with lots of
arrays inside.

TIA,
Chuck


Chuck Ritzke said:
Thanks Patrick,

I looked further and realized that my problem happens because the value
inside the structure are arrays. Arrays inside the structure become
references to each other whereas primitive variables stay independent. It
sure would help to have a quick fix to handle this difference between VB6
and .NET.

So modifying your example...

Structure MyDataType
Dim val1() As Double
Dim val2 As Double
End Structure

Module Module1

Sub Main()
Dim d1 As MyDataType
Dim d2 As MyDataType
reDim d1.val1(1)
reDim d2.val1(1)

d1.val1(1) = 11
d2.val1(1) = 22
d1.val2=44
d2.val2=55
'AT THIS POINT BOTH ARRAYS AND VARIABLES INSIDE d1 and d2 ARE
INDEPENDENT

d2=d1

d2.val1(1) = 999
d2.val2=888
'NOW THE RESULT IS
'd1.val(1)=999 and d2.val(1)=999 and now reference each other
whereas...
'd1.val2=55 and d2.val2=88 are still independent

End Sub

End Module


if
When
 
"Chuck Ritzke" said:
It looks like I can write a "cloning" function for each UDT to handle
arrays...

In .NET you could implement the ICloneable interface and get basically
the same results.

But, in the end, you will have to copy the arrays manually when copying
the structs (types). This is known as a "deep copy". By default, you
get a "shallow copy" where only the reference to the array is copied --
thus making both structs point to a single array.
 
Back
Top