Napivo1972 Posted May 10, 2005 Posted May 10, 2005 I ran into a strange problem. When I use this code I get the output in the console: Before move test.Player.Pion.pos = 0 After test.Player.Pion.pos = 0 Before move test.Player.Pion.pos = 0 After test.Player.Pion.pos = 0 As you see m_pos is not augmented� Actually it is but somehow the data reverts to it�s old value upon exiting. Public Class TestClass Private m_Player As sPlayer Public Structure sPion Private m_pos As Integer Public Sub New(ByVal Pos As Integer) m_pos = Pos End Sub Public Sub move(ByVal pos As Integer) m_pos += pos End Sub Public ReadOnly Property pos() As Integer Get Return m_pos End Get End Property End Structure Public Structure sPlayer Private m_test As sPion Private m_name As String Public Sub New(ByVal vName As String) m_test = New sPion(0) m_name = vName End Sub Public ReadOnly Property Pion() As sPion Get Return m_test End Get End Property End Structure Public Sub New() m_Player = New sPlayer("test") End Sub Public ReadOnly Property Player() As sPlayer Get Return m_Player End Get End Property Public Sub main() Dim test As New TestClass Do Console.WriteLine("Before move test.Player.Pion.pos = " & test.Player.Pion.pos) test.Player.Pion.move(5) Console.WriteLine("After test.Player.Pion.pos = " & test.Player.Pion.pos) Loop End Sub End Class Now when I change Public Structure sPion to Public Class sPion The output becomes Before move test.Player.Pion.pos = 0 After test.Player.Pion.pos = 5 Before move test.Player.Pion.pos = 5 After test.Player.Pion.pos = 10 So now it works.. but why? Can anyone explain to me why this is happening? So that the next time I have a problem like this it will not take me 4 days to track it down.. Thanks Quote
Napivo1972 Posted May 11, 2005 Author Posted May 11, 2005 Just a bump I'd realy like to know what is happening here..... hopefully a smart guy passes today. Quote
Administrators PlausiblyDamp Posted May 11, 2005 Administrators Posted May 11, 2005 http://www.xtremedotnettalk.com/showthread.php?t=92354 appears to cover a similar situation with using structures. http://objectsharp.com/Blogs/bruce/archive/2004/08/04/796.aspx discusses what is going on behind the scenes a bit and why this happens. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Napivo1972 Posted May 11, 2005 Author Posted May 11, 2005 While looking a bit further myself I came across this tutorial on the code project. http://www.codeproject.com/dotnet/Structures_VBNet.asp It tells that structures are Value types and classes are reference type. I think, and I need a little confirmation, that when I get test.Player.Pion I get a copy or clone of the Peon structure instead of a reference. When I then change the value and exit the sub the reference to that peon is lost and gets deleted. When I try and read the value back I get another copy of the peon with its old value�. Does that sound acceptable (and correct) as an explanation? Quote
Machaira Posted May 11, 2005 Posted May 11, 2005 You got it. You're just changing a copy of the data and not the actual data itself. Quote Here's what I'm up to.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.