setting object property in an arraylist

  • Thread starter Thread starter Osmosis
  • Start date Start date
O

Osmosis

I have a simple class like this
Structure Eventitem
Public logdate As DateTime

Public recurring As Byte

Public Description As String

End Structure

I create several objects of this type and keep them in an arraylist named
'history'

When I try to assign a new value to one of the descriptions with the
following code :

CType(history(0), Eventitem).Description = "popo"

I get the following error message : Expression is a value and therefore
cannot be the target of an assignment.

I'm fairly new to VB.NET, but this seems like valid code to me. I found a
similar example at
http://samples.gotdotnet.com/quickstart/howto/doc/clone.aspx with the
following line : CType(nl(0), Employee).Name = "Mary Smith"


Does someone have a clue why I'm getting this error ?



Thanks in advance,

Osmosis
 
Osmosis said:
I have a simple class like this
Structure Eventitem
Public logdate As DateTime

Public recurring As Byte

Public Description As String

End Structure

I create several objects of this type and keep them in an arraylist
named 'history'

When I try to assign a new value to one of the descriptions with
the following code :

CType(history(0), Eventitem).Description = "popo"

I get the following error message : Expression is a value and
therefore cannot be the target of an assignment.

I'm fairly new to VB.NET, but this seems like valid code to me. I
found a similar example at
http://samples.gotdotnet.com/quickstart/howto/doc/clone.aspx with
the following line : CType(nl(0), Employee).Name = "Mary Smith"


Does someone have a clue why I'm getting this error ?


A structure is a value type (in opposite to a reference type). When you get
the item back by calling history(0), you get a *copy* of the structure in
the arraylist. Setting a property of the *copy* wouldn't make sense,
therefore the error. There is no solution - apart from using a class instead
of a structure.
 
Your Eventitem must be a class and not a structure. Then it will work.

Public Class Eventitem
Public logdate As DateTime
Public recurring As Byte
Public Description As String
End Class
 
Back
Top