Nest ForEach循环不更新对象 [英] Nest ForEach Loops don't update object

查看:76
本文介绍了Nest ForEach循环不更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次遇到过这个问题,我确信这与我缺乏理解有关。如果有人能够解释这个问题以及为什么它不能达到我的预期,以及如何正确构建代码,我将非常感激。我剪掉了部分,但你应该能够理解我在做什么。



我有一个ForEach循环嵌套在另一个ForEach循环内部嵌套,虽然看起来成功迭代了对象,但是不能更新它?我使用手表,断点,......一切都很好,可变时间正确更新并具有正确的值。 current_pvt是正确的,current_move是正确的,但是在调用之后

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



我检查current_pvt.time值,它与赋值之前的值相同,或者与之前current_move的current_pvt.time值相同?我无法确定,因为它们在声明之前是相同的。内存服务于早期尝试使用第一个current_move值写入的不同代码。



是什么给出的?我在编写这个嵌套的ForEach时已经看到了这一点,所以显然我在做一些根本错误的事情。通常第一次通过内部循环它正确更新,但以下时间,它没有,所以我认为它与对象没有被更新,销毁,正确更新?我应该如何使用两个不同的对象编写这样的嵌套ForEach循环?



谢谢,



Kennard






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

推荐答案

我认为你需要更仔细地看一下 - 我怀疑问题更可能是你没有编译代码。

具体来说,你显示的代码不会编译,因为timeslice不是_move类的成员。



你所显示的代码有一些(可能)重要的是缺少,或者它没有被编译,你试图找到你的代码中的错误,那不是在那里!



尝试在外循环之前放置一个断点并单步执行 - 我打赌调试器抱怨代码与源不匹配!
I think you need to look at this a bit more closely - I suspect that the problem is more likely to be that you haven''t compiled the code.
Specifically, the code you show won''t compile, because "timeslice" is not a member of the _move class.

Either your code as shown has some (possibly important) bits missing, or it hasn''t been compiled, and you are trying to find a bug in your code, that isn''t in there!

Try putting a breakpoint just before the outer loop and single stepping through - I bet the debugger complains that the code doesn''t match the source!


啊,找到原因并解决了问题另一个网站。



Prog.PVT是一个结构,当你使用一个结构时,因为它是一个值类型,我得到它的副本,而不是原本的。我需要将其更改为Class才能迭代和更改项目。如果没有,那么我上面发布的方法将起作用,因为它为列表创建了一个新项目并用它替换旧项目。
Ah, found the reason and solution over on another site.

Prog.PVT is a structure, when you work with a structure, since it is a value type, I get a copy of it, not the original. I need to change it to a Class in order to iterate and change the items. If not, then the method I posted above would work since it creates a new item for the list and replaces the old one with it.


这篇关于Nest ForEach循环不更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆