开发者

Update Property of Object in list in ForEach

开发者 https://www.devze.com 2023-02-21 20:06 出处:网络
I have a List(Of SomeObject) that I enumerate over using a For Each loop. This SomeObject contains a Property that references another Object, as such

I have a List(Of SomeObject) that I enumerate over using a For Each loop. This SomeObject contains a Property that references another Object, as such

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

The question I have is whether updating AnotherObject property of SomeObject is valid within a For Each loop.

As such

Dim newanother As New AnotherObject()

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

Is this valid? If not, what is a valid way to d开发者_StackOverflow中文版o it? (ordinary for loop perhaps?)


you're just changing the value of a property that is correct!!!, you can doit inside a cycle scope or not, beeing your proprty type a object or a system type.

what you cannot do, is change the collection inside the iteration scope!

0

精彩评论

暂无评论...
验证码 换一张
取 消