我在列表中有一个用户定义的结构,我试图更改结构列表中单个元素的值。访问元素不是问题。但是,当我尝试更新值时,编译器会提示:
"Expression is a value and therefore cannot be the target of the assignment"
例如:
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
End Structure
_
Public Sub ListTest()
Dim newPerson as Person
Dim records as List (Of Person)
records = new List (Of Person)
person.first = "Yogi"
person.last = "bear"
person.age = 35
records.Add(person)
records(0).first = "Papa" ' <<== Causes the error
End Sub
最佳答案
我遇到了同样的问题,我通过向更改属性值的结构添加一个简单的 Sub 来修复它。
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
Public Sub ChangeFirst(value as String)
me.first = value
End Sub
End Structure
关于vb.net - 如何在 VB.NET 中为 List 中的结构元素赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126318/