结构中的属性:"表达式是一个值,因此不能成为赋值的目标."

ser*_*hio 8 .net vb.net structure

我有以下2种结构,我真的不明白为什么第二种结构不起作用:

Module Module1    
  Sub Main()
    Dim myHuman As HumanStruct
    myHuman.Left.Length = 70
    myHuman.Right.Length = 70

    Dim myHuman1 As HumanStruct1
    myHuman1.Left.Length = 70
    myHuman1.Right.Length = 70    
  End Sub

  Structure HandStruct
    Dim Length As Integer
  End Structure

  Structure HumanStruct
    Dim Left As HandStruct
    Dim Right As HandStruct
  End Structure

  Structure HumanStruct1
    Dim Left As HandStruct
    Private _Right As HandStruct
    Public Property Right As HandStruct
      Get
        Return _Right
      End Get
      Set(value As HandStruct)
        _Right = value
      End Set
    End Property    
  End Structure    
End Module
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更详细的解释:我有一些使用结构而不是类的过时代码.因此,我需要确定此结构的字段更改为错误值的时刻.

我的调试解决方案是用同名的属性替换结构域,然后我只在属性setter中设置一个breackpoint来识别我收到错误值的那一刻......为了不重写所有的代码....仅用于调试目的.

现在,我遇到了上面的问题,所以我不知道该怎么办...只在每个结构成员分配的地方设置断点,但是有很多行都有这个任务......

Kev*_*lia 9

这只是运行程序时发生的事情的问题.getter返回结构的副本,在其上设置一个值,然后该结构的副本超出范围(因此修改后的值不会执行任何操作).编译器将此显示为错误,因为它可能不是您的意图.做这样的事情:

Dim tempRightHand as HandStruct
tempRightHand = myHuman.Right
tempRightHand.Length = 70
myHuman.Right = tempRightHand
Run Code Online (Sandbox Code Playgroud)

左边是有效的,因为你是直接访问它而不是通过属性访问它.

  • 是否会将所有结构更改为类? (2认同)