Expression is a value and therefore cannot be the target of an assignment

Public Structure _cache
    Public loading As Boolean
End Structure

Public Class Server
    Public _cache_List As New List(Of _cache)()

For index As Int16 = 0 To _cache_List.Count - 1
_cache_List(index).loading = false '--> error

"Expression is a value and therefore cannot be the target of an assignment "

what was wrong in the code above?
Who is Participating?
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Structures are fundamentally different from classes in that they are value-types rather than reference types. Whether or not you need a struct rather than a class depends largely on its intended use.
Carl TawnSystems and Integration DeveloperCommented:
The problem is that you are making an indirect reference to a Value-type (your structure) which results in it being classed as an expression.
You will have to declare a variable to hold a direct reference to the item and assign through that:

For index As Int16 = 0 To _cache_List.Count - 1
   Dim item As _cache = _cache_list(index)
   item.loading = false

Open in new window

JSW21Author Commented:
Hi there
So I change the Structure into Class and it works.
I didnt know using structure will have different from class.

Do you think i am doing the right thing.
JSW21Author Commented:
I will need to use class as I need to modify its values.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.