serhio
serhio

Reputation: 28586

Properties in Structures: "Expression is a value and therefore cannot be the target of an assignment."

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

enter image description here

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.

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...

Upvotes: 8

Views: 10733

Answers (2)

Tony
Tony

Reputation: 121

I ran into the Same problem... The solution is to define operators in the Struct, specifying what to do with an expression like:

[StructType]=[StructType] '+-*/' [Integer (or whatever)]

For yours I defined the '&' operator to overwrite the 'Length' value in [HandStruct]... Then return a new instance of [HandStruct].

[HandStruct]=[HandStruct] & [Integer] 'overwrites length value, returns new instance

I also added '+','-' operators for another example.

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      '< problem code


        myHuman1.Right &= 70             '< using the '&' operator, Defined as OVERWRITE  ... [HandStruct]=[HandStruct] & [Integer]
        myHuman1.Right += 1              '< using the +/- operators
        myHuman1.Right -= 3

    End Sub

    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


    Structure HandStruct

        Dim Length As Integer


        '******* OPERATORS (SHARED) ********************************************************
        '****
        '**
        ' '&' Overwrite Length value (MAIN)
        '       [HandStruct] = [HandStruct] & [Integer]
        Public Shared Operator &(ByVal myStruct As HandStruct,
                                 ByVal i_val As Integer) As HandStruct
            myStruct.Length = i_val
            Return myStruct
        End Operator
        '--- flip operands
        '       [HandStruct] = [Integer] & [HandStruct]
        Public Shared Operator &(ByVal i_val As Integer, ByVal myStruct As HandStruct) As HandStruct
            Return myStruct & i_val
        End Operator
        '--------------------------------
        '--------------------------------
        ' '+' add to Length (MAIN)
        '       [HandStruct] = [HandStruct] + [Integer]
        Public Shared Operator +(ByVal myStruct As HandStruct,
                                 ByVal i_val As Integer) As HandStruct
            myStruct.Length += i_val
            Return myStruct
        End Operator
        '--- flip operands
        '       [HandStruct] = [Integer] - [HandStruct]
        Public Shared Operator +(ByVal i_val As Integer, ByVal myStruct As HandStruct) As HandStruct
            Return myStruct + i_val
        End Operator
        '--------------------------------
        '--------------------------------
        ' '-' subtract from Length (MAIN)
        '       [HandStruct] = [HandStruct] - [Integer]
        Public Shared Operator -(ByVal myStruct As HandStruct,
                                 ByVal i_val As Integer) As HandStruct
            myStruct.Length -= i_val
            Return myStruct
        End Operator
        '--- flip operands
        '       [HandStruct] = [Integer] - [HandStruct]
        Public Shared Operator -(ByVal i_val As Integer, ByVal myStruct As HandStruct) As HandStruct
            Return myStruct - i_val
        End Operator

    End Structure


End Module

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26078

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.

Upvotes: 9

Related Questions