Results 1 to 6 of 6

Thread: [RESOLVED] Problem with changing data in a structure

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Resolved [RESOLVED] Problem with changing data in a structure

    Hi all,

    I have some structures and I change the data inside a loop. This works ok. But as soon as the method ends, all values in the structure are reset???

    Does anyone have an idea why this is?

    Heres my code:

    Code:
    Private Sub ReadDevice(ByRef device As Device)
            Console.WriteLine("     Reading device " & Device.name)
    
            Dim data As Short
            PLC.ReadDeviceBlock2(Device.name, 1, data)
            Dim temp = Convert.ToString(data, 2).ToCharArray
            Array.Reverse(temp)
            Dim word = Convert.ToString(temp)
    
            Console.WriteLine("          Word : " & word)
    
            For Each Element As Element In Device.elements
                Dim bitStatus As Boolean = Convert.ToBoolean(Convert.ToByte(word(Element.index).ToString))
                If bitStatus.Equals(True) AndAlso Element.status.Equals(False) Then
                    Element.status = True
                    Element.timestamp = Now
                ElseIf bitStatus.Equals(False) AndAlso Element.status.Equals(True) Then
                    Element.status = False
                    Element.totalSeconds = Convert.ToInt32((Now - Element.timestamp).TotalSeconds)
                End If
                Console.WriteLine("Index : " & String.Format("{0:00}", Element.index) & "  Status : " & Element.status & "  Timestamp : " & Element.timestamp.TimeOfDay.ToString & "  TotalSeconds : " & Element.totalSeconds)
            Next
    
        End Sub
    
        Private Structure Lane
            Dim description As String
            Dim devices As List(Of Device)
        End Structure
    
        Private Structure Device
            Dim name As String
            Dim elements As List(Of Element)
        End Structure
    
        Private Structure Element
            Dim index As Byte
            Dim status As Boolean
            Dim timestamp As DateTime
            Dim totalSeconds As Integer
        End Structure
    
    End Module
    Don't pay too much attention at what is going on. It's just that when I assign a value: Element.TimeStamp = Now it will be 00/00/0000 12:00:00 again when I exit the sub :S.


    [EDIT]
    Method that calls ReadDevice() :
    Code:
        Private Sub ReadLane(ByRef lane As Lane)
            Console.WriteLine(Environment.NewLine & "Reading lane " & Lane.description)
            For Each device As Device In Lane.devices
                ReadDevice(device)
            Next
        End Sub
    Last edited by gonzalioz; Nov 25th, 2009 at 04:45 AM.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Problem with changing data in a structure

    Structures are value types. When you retrieve an instance of a structure from some collection, you are actually making a copy of it's value. You can then change that value all you like, but the original structure, which is still in the collection, is never touched.

    Instead, use a Class. Classes are reference types, which means that if you retrieve an instance of a class from some collection, you are retrieving a copy to its memory location. If you change that instance, you are actually modifying the class instance on that copied memory location (which points to the very same object) so you are modifying the instance in the collection, like you want.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with changing data in a structure

    Quote Originally Posted by NickThissen View Post
    Structures are value types. When you retrieve an instance of a structure from some collection, you are actually making a copy of it's value. You can then change that value all you like, but the original structure, which is still in the collection, is never touched.
    Quite true.
    Quote Originally Posted by NickThissen View Post
    Instead, use a Class. Classes are reference types, which means that if you retrieve an instance of a class from some collection, you are retrieving a copy to its memory location. If you change that instance, you are actually modifying the class instance on that copied memory location (which points to the very same object) so you are modifying the instance in the collection, like you want.
    That's one solution but you can certainly still edit structure instances in a collection, e.g.
    vb.net Code:
    1. Dim instance As SomeStructure = myList(index)
    2.  
    3. instance.SomeProperty = someValue
    4. myList(index) = instance
    You simply overwrite the original with the copy once you're done editing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with changing data in a structure

    It should also be noted that, using the method I suggested, the original code would have to be changed to use a For loop rather than a For Each loop, providing the ability to assign back to the collection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Problem with changing data in a structure

    K, thnx! I got it to work now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width