Properties in structures: "An expression is a value and therefore cannot be the purpose of an assignment."

I have the following 2 structures, and I really don't 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 

enter image description here

More detailed explanation: I have legacy code that uses structures instead of classes. Therefore, I need to determine the moment when the correction of this structure will change to the wrong value.

My debugging solution was to replace the structure filed by a property with the same name, and then I just set a breakpoint in the property adjuster to determine when I get the wrong value ... so as not to rewrite all the code .... only for the purpose of debugging.

Now I am faced with the problem above, so I do not know what to do ... only setting a breakpoint wherever this member of the structure is assigned, but there are many lines with this destination ...

+6
source share
1 answer

This is just a matter of what happens when the program starts. The recipient returns a copy of your structure, you set a value on it, then this copy of the structure goes out of scope (therefore, the changed value does nothing). The compiler shows this as an error, since this 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 one works because you are accessing it directly, not through a property.

+6
source

Source: https://habr.com/ru/post/950917/


All Articles