在运行列表上的函数之后,我想更新列表的每个元素。目前,我正在使用for each
循环,但是更改没有保存...是否有一个版本传递了每个变量byref
以便保留更改。
例如,目前:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles Button2.Click
Dim letters() As String = {"a", "b", "c"}
Debug.Write(letters(1))
For Each letter As String In letters
letter = "d"
Next
Debug.Write(letters(1))
End Sub
打印bb
,但我希望它打印bd
答案 0 :(得分:1)
如果您的示例将使用引用类型列表,则它将按书面形式工作。字符串是不可变的。更改一个的唯一方法是创建一个新的,并指向您的引用。
答案 1 :(得分:0)
我修改了我的测试脚本,以免在@the_lotus的注释之后使用不可变字符串的列表,并且以下内容现在表现出预期的效果:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles Button2.Click
Dim lines As New List(Of clsDepLine)
lines.Add(New clsDepLine)
lines.Add(New clsDepLine)
lines(0).Account_Manager = "A"
Debug.Write(lines(0).Account_Manager)
For Each line As clsDepLine In lines
line.Account_Manager = "b"
Next
Debug.Write(lines(0).Account_Manager)
End Sub
输出Ab
供参考,定义了clsDepLine:
Public Class clsDepLine
Public Property Account_Manager As String
[...]more properties[...]
End Class