在ForEach中更新列表中Object的属性

时间:2011-04-02 13:39:00

标签: vb.net foreach

我有List(Of SomeObject)我使用For Each循环枚举。这个SomeObject包含一个引用另一个Object的Property,因此

Class SomeObject
Public Property Another As AnotherObject
Get
    Return _another
End Get
Set (byval value as Integer)
_another = value
End Set
End Property

dim _another As New AnotherObject()
End Class

我遇到的问题是,在For Each循环中更新SomeObject的AnotherObject属性是否有效。

就这样

Dim newanother As New AnotherObject()

For Each el As SomeObject in myobjects
  el.Another = newanother
Next

这有效吗? 如果没有,那么有效的方法是什么? (也许普通的循环?)

1 个答案:

答案 0 :(得分:4)

你只是改变了一个正确的属性的值!!!,你可以在一个循环范围内做,或者不做你自己的类型对象或系统类型。

你不能做的是改变迭代范围内的集合!