Click here to Skip to main content
15,897,891 members

Nest ForEach Loops don't update object

Makulais asked:

Open original thread
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
VB
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


VB
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900