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 4 2489
In article <Ou**************@TK2MSFTNGP12.phx.gbl>, "Chuck Ritzke"
<CHUCK AT MYACTUARY DOT COM> says... 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
--
Patrick Steele
Microsoft .NET MVP http://weblogs.asp.net/psteele
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
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om... In article <Ou**************@TK2MSFTNGP12.phx.gbl>, "Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> says... 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
-- Patrick Steele Microsoft .NET MVP http://weblogs.asp.net/psteele
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" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:uk*************@tk2msftngp13.phx.gbl... 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
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message news:MP************************@msnews.microsoft.c om... In article <Ou**************@TK2MSFTNGP12.phx.gbl>, "Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> says... 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
-- Patrick Steele Microsoft .NET MVP http://weblogs.asp.net/psteele
In article <#k**************@TK2MSFTNGP11.phx.gbl>, "Chuck Ritzke"
<CHUCK AT MYACTUARY DOT COM> says... 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.
--
Patrick Steele
Microsoft .NET MVP http://weblogs.asp.net/psteele This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Aaron Queenan |
last post by:
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
|
by: Charles Law |
last post by:
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is...
|
by: rawCoder |
last post by:
Hi,
Just wanted to know if there is any speed difference between
VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc.
..NETs Convert.To<...> methods.
And which is better to be...
|
by: Qwert |
last post by:
Hello,
is it correct that if a type (System.Type) is a value (.IsValueType=True)
and not primitive (.IsPrimitive=False), that type is a structure?
Thanks.
|
by: active |
last post by:
RecIn is a structure
FileGet(FileNum, RecIn, mNumb)
works if I have
Option Strict Off
File was written by
|
by: HONOREDANCESTOR |
last post by:
I tried to read a structure from a file, but I got this error message
before I could compile it:
"Error 103 Option Strict On disallows narrowing from type
'System.ValueType' to type...
|
by: =?Utf-8?B?Y2xhcmE=?= |
last post by:
Hi all,
how to understand the following declaration in MSDN about nullable structure
<SerializableAttribute_
Public Structure Nullable(Of T As Structure)
I can : dim id as nullable(of...
|
by: eBob.com |
last post by:
In my class which contains the code for my worker thread I have ...
Public MustInherit Class Base_Miner
#Region " Delegates for accessing main UI form "
Delegate Sub DelegAddProgressBar(ByVal...
|
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= |
last post by:
I know structure is a value type and derived from System.ValuType. So I have
the following questions,.
1 - C# won't allow to derive one structure from other. Then how come it is
derived from...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
| |