473,657 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB6 conversion of UDT to structure - ValueType?

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

Nov 20 '05 #1
4 2507
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
Nov 20 '05 #2
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.o rg> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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

Nov 20 '05 #3
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(MyDa ta.Val1)
'and repeat last line for each array in MyDataType

Return MyCopy

End Function

'Then instead of d2=d1

d2=CloneMyDataT ype(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******** *****@tk2msftng p13.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.o rg> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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


Nov 20 '05 #4
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
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
12760
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
15
8225
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 also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns "Default caption". Can anyone tell me why, and how I can make this work? <code> Imports System
47
2852
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 used and why ? Thanx
10
1910
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.
5
2634
by: active | last post by:
RecIn is a structure FileGet(FileNum, RecIn, mNumb) works if I have Option Strict Off File was written by
2
1604
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 'OptionsRecordType' in copying the value of 'ByRef' parameter 'Value' back to the matching argument." The 3 lines of code are: If My.Computer.FileSystem.FileExists(strPath) Then FileOpen(1, strPath, OpenMode.Binary) FileGet(1, OptionsRecord) The third line...
3
1425
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 integer), but integer is not a structure, so what is the function of the AS Structure here?
4
1717
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 uiform As Form1, ByRef si As MTCC02.Form1.SiteRunOpts, _ ByVal numitems As Integer) #End Region
7
1521
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 System.ValueType ? 2 - I have written a simple C# console application. Following is the equivalent IL code which is generated for the structure ..class sequential ansi sealed nested private beforefieldinit MyStruct
0
8392
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8726
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.