Property Set procedure with a structure array

M

Mark Raishbrook

Apologies in advance for what is a very simple question, but I just cannot
get my head around it. I have a public class Something with a public
structure T mapped to the local variable m_Test(). Whenever I try to set the
value of the property Test, I get the error "Expression is a value and
therefore cannot be the target of an assignment." Test code is below. Can
anyone tell me what I'm doing wrong?

Public Class Something

Public Structure T
Public a As Integer
Public b As Integer
End Structure

Private m_Test() As T
Private m_CurrentIndex As Integer

Public Property CurrentIndex() As Integer
Get
Return m_CurrentIndex
End Get
Set(ByVal Value As Integer)
m_CurrentIndex = Value
If Value > m_Test.GetLength(0) - 1 Then
ReDim Preserve m_Test(Value)
End If
End Set
End Property

Public Property Test() As T
Get
Return m_Test(m_CurrentIndex)
End Get
Set(ByVal Value As T)
m_Test(m_CurrentIndex).a = Value.a
m_Test(m_CurrentIndex).b = Value.b
End Set
End Property

End Class

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim s As Something
s.CurrentIndex = 1
s.Test.a = 2 '* Error!!!

End Sub

End Class
 
A

Anthony Jones

Mark Raishbrook said:
Apologies in advance for what is a very simple question, but I just cannot
get my head around it. I have a public class Something with a public
structure T mapped to the local variable m_Test(). Whenever I try to set the
value of the property Test, I get the error "Expression is a value and
therefore cannot be the target of an assignment." Test code is below. Can
anyone tell me what I'm doing wrong?

Public Class Something

Public Structure T
Public a As Integer
Public b As Integer
End Structure

Private m_Test() As T
Private m_CurrentIndex As Integer

Public Property CurrentIndex() As Integer
Get
Return m_CurrentIndex
End Get
Set(ByVal Value As Integer)
m_CurrentIndex = Value
If Value > m_Test.GetLength(0) - 1 Then
ReDim Preserve m_Test(Value)
End If
End Set
End Property

Public Property Test() As T
Get
Return m_Test(m_CurrentIndex)
End Get
Set(ByVal Value As T)
m_Test(m_CurrentIndex).a = Value.a
m_Test(m_CurrentIndex).b = Value.b
End Set
End Property

End Class

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim s As Something
s.CurrentIndex = 1
s.Test.a = 2 '* Error!!!

End Sub

End Class

The problem is that s.Test returns a _copy_ of what is found in
m_Test(m_CurrentIndex). This happens because structures are value types.
Hence s.Test.a = 2 would cause 2 to be assigned to the copy of the T
structure returned by .Test which will be garbage and soon as the expression
is complete. Since this would be a completely useless thing to do the
compilier disallows it.

Dim t As Something.T

t = s.Test
t.a = 2
s.Test = t
 
M

Marc Gravell

As an aside - mutable structs are generally a bad idea for this reason an a
dozen others... it sounds like this perhaps should be a class (or
immutable).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top