XardasLord
XardasLord

Reputation: 1947

Structure property - Expression Is a value and therefore cannot be the target of an assignment

I have two classes:

Public Class SubiektGT
    Dim daneKlienta As SklepPobieranieDanych = New SklepPobieranieDanych(Me)

    Public Sub PrzefiltrujDaneKontrahenta()
    If daneKlienta.DaneKontrahenta.adres_fv = "" Then
        daneKlienta.DaneKontrahenta.adres_fv = daneKlienta.DaneKontrahenta.adres_wys 'ERROR
    End If
End Sub
End Class



Public Class SklepPobieranieDanych
    Public Structure Kontrahent
        Public adres_wys As String
        Public adres_fv As String
    End Structure

    Private _daneKontrahenta As Kontrahent

    Public Property DaneKontrahenta() As Kontrahent
    Get
        Return _daneKontrahenta
    End Get
    Set(value As Kontrahent)
        _daneKontrahenta = value
    End Set
End Property

    Public Sub PobierzTowaryKontrahenta()
        _daneKontrahenta.adres_fv = ""
        _daneKontrahenda.adres_wys = "a"
    End Sub
End Class

And when I try to assign a value from a different class to daneKlienta.DateKontrahenta.adres_fv I get following error: Expression Is a value and therefore cannot be the target of an assignment.

So how can I assign a value from that class? It is really important to me to do it this way and please someone explain me why it happens. Thanks!

Upvotes: 1

Views: 689

Answers (1)

The compiler is referring to your Kontrahent structure.

Because it is a structure and not a class, in the construct

daneKlienta.DaneKontrahenta.adres_fv =...

DaneKontrahenta is actually a copy of the property held in daneKlienta.

That is, the implicit get function being invoked at the first dot to get DaneKontrahentafrom the class returns a copy, not a reference (since Kontrahent is not a reference type).

So the assigned value would be immediately thrown away afterwards along with the temporary copy of DaneKontrahenta.

Does that make sense?

Upvotes: 2

Related Questions