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:
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.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
[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




Reply With Quote