Results 1 to 6 of 6

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

Threaded View

  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.

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