加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在VB.NET中为List中的结构元素分配值?

发布时间:2020-12-17 00:09:50 所属栏目:大数据 来源:网络整理
导读:我在列表中有一个用户定义的结构,我试图在结构列表中的单个元素中更改值.访问元素不是问题.但是,当我尝试更新该值时,编译器抱怨: “Expression is a value and therefore cannot be the target of the assignment” 例如: Public Structure Person Dim fir
我在列表中有一个用户定义的结构,我试图在结构列表中的单个元素中更改值.访问元素不是问题.但是,当我尝试更新该值时,编译器抱怨:

“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
正如其他评论所说,当您引用记录(0)时,您将获得一个结构体的副本,因为它是一个值类型.你可以做什么(如果你不能把它改成一个类)就是这样的:
Dim p As Person = records(0)
p.first = "Papa"
records(0) = p

虽然,我认为使用一个类更简单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读