I have come across this issue multiple times, and I'm sure it has something to do with my lack of understanding. If someone would be kind enough to explain the problem and why it doesn't do what I expect, and how to properly frame the code, I would greatly appreciate it. I cut out sections but you should be able to understand what I'm doing.
I have a ForEach loop nested inside another ForEach loop, the inside nest, although it appears to successfully iterate through the object, won't update it? I use watches, breakpoints, ... it all shows fine, the variable time is updating correctly and has the correct value. The current_pvt is correct, the current_move is correct, but after the call
current_pvt.time = time
I check the current_pvt.time value and it's the same as it was previous to the assignment or the same as the current_pvt.time value for the previous current_move? I can't tell for sure as they are the same prior to the statement. Memory serves from earlier attempts on different code that is writes it with the first current_move value.
What gives? I have seen this while writing this nested ForEach so apparently I am doing something fundamentally wrong. Typically the first time through the inner loop it updates correctly, but the following times, it does not, so I assume it has to do with the object not be updated, destroyed, renewed properly? How should I write a nested ForEach Loop like this with two different objects?
Thank you,
Kennard
Public Class _program
Public programname As String
Public moves As List(Of _move)
Public Sub New()
progglobals = New _globals
moves = New List(Of _move)
End Sub
End Class
Public Structure PVT
Public X As Double
Public Y As Double
Public Xvel As Double
Public Yvel As Double
Public time As Double
End Structure
Public Class _move
Public moveID As Integer
Public pvtPts As New List(Of PVT)
Public timeslice As Double
End Class
Private Sub SetPVTTimes(ByRef current_program As Prog._program)
Dim time As Double = 0.0
For Each current_move As Prog._move In current_program.moves
If current_move.pvtPts.Count = 0 Then
time = 0.0
Else
For Each current_pvt As Prog.PVT In current_move.pvtPts
current_pvt.time = time
time = time + current_move.timeslice
Next
End If
Next
End Sub