Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...