在结构特性:"实例pression是一个值,因此不能作为赋值的靶QUOT;是一个、赋值、实例、特性

2023-09-05 00:38:21 作者:给山河的信

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

I have the following 2 structures, and I don't really understand why the second one does not work:

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

更详细的解释:我有一个使用的结构,而不是类的一些过时的code。所以,我需要确定一个时刻,当一提起这个结构的变化,以错误的值。

More detailed explanation: I have some obsolete code that uses structures instead of classes. So I need to identify a moment when a filed of this structure changes to the wrong value.

我的解决办法调试是更换相同名义提交由产权结构,然后我就在属性的setter,以确定当我收到错误的值的时刻breackpoint ......为了不重写所有code ....只是用于调试的目的。

My solution to debug was to replace the structure filed by a property with the same name, and then I just set a breackpoint in the property setter to identify the moment when I receive the wrong value... in order do not rewrite all the code.... just for debugging purpose.

现在,我所面临的问题上面,所以我不知道该怎么办......只能到处设置结构的该成员被分配断点,但有很多线路与分配对象...

Now, I faced the problem above, so I don't know what to do... only setting the breakpoint everywhere this member of structure is assigned, but there is a lot of lines with that assignment...

推荐答案

这是当你运行程序所发生的事情只是一个问题。吸气返回你的结构的副本,你就可以设置一个值,则该结构的副本超出作用域(使修改后的值不会做任何事情)。编译器显示这是一个错误,因为它可能不是您所希望的。做这样的事情:

It's just a matter of what is happening when you run the program. The getter returns a copy of your struct, you set a value on it, then that copy of the struct goes out of scope (so the modified value doesn't do anything). The compiler shows this as an error since it is probably not what you intended. Do something like this:

Dim tempRightHand as HandStruct
tempRightHand = myHuman.Right
tempRightHand.Length = 70
myHuman.Right = tempRightHand

左的作品,因为你访问它,而不是直接通过属性。

The left works because you are accessing it directly instead of through a property.