locked
Problem with updating generic list RRS feed

  • Question

  • User-1050023416 posted

    I have a generic list of objects. Each object is a simple structure that holds three values. I want to update one of those values for each item in the list. However the code below does not seem to work:

    For Each tacr As acrElement In acrlist
    tacr.ElementText =
    "hello"
    Next

    For clarity: acrlist is a list(of acrElement)

    The code executes without any errors but the content of acrlist is unchanged.

    Thanks,

    Adrian.

    Thursday, January 22, 2009 9:42 AM

Answers

  • User1868789274 posted

    YOu have declared a structure and not a class. you need to do something like this:
    <Serializable()> _

    Public Class acrElement(Of T)

    Implements IList(Of T),

    private _ElementText

    Public Property ElementText as String

    Get

    Return _ElementText

    End Get

    Set (value as String)

    _Element Text= value

    End Set

    End Class

     

    now this will work:

    Dim acrlist as IList(Of T)

    For i = 0 To acrlist.Count - 1
    acrlist(i).ElementText =
    "hello"
    Next

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 24, 2009 10:50 AM

All replies

  • User968497051 posted

    Hi,

    Do not use ForEeach in this case , use For Loop instead (or While Loop) ..

     

    Friday, January 23, 2009 12:45 AM
  • User-1050023416 posted

    Sorry, I am not sure how changing the type of loop will help. Could you provide me with an example?

     Kind regards,

    Adrian.

    Friday, January 23, 2009 4:05 AM
  • User968497051 posted

     

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myGenericList As List(Of Integer) = New List(Of Integer)
            myGenericList.Add(1)
            myGenericList.Add(2)
            myGenericList.Add(3)
            myGenericList.Add(4)
    
    
            For i As Integer = 0 To myGenericList.Count - 1
                myGenericList(i) = myGenericList(i) * 2
            Next
    
            For i As Integer = 0 To myGenericList.Count - 1
                Label1.Text += myGenericList(i).ToString()
            Next
    
        End Sub
     Hi,

    This code creates a generic list of integer , changes its values and then print them

     

    Friday, January 23, 2009 7:21 AM
  • User1330954201 posted
    I think you only get the value of the object in a for each loop and not a reference to it, so you can't actually change the object in the original list.
    Friday, January 23, 2009 12:03 PM
  • User1868789274 posted

    What kind of error are you getting?

    Friday, January 23, 2009 3:16 PM
  • User-1050023416 posted

    Here is my sample code:

    For i = 0 To acrlist.Count - 1
    acrlist(i).ElementText =
    "hello"
    Next

    In VS I am getting 'Expression is a value and therefore cannot be the target of an assignment.

    acrlist is a collection of acrElement. acrElement is defined as:

    <Serializable()> Public Structure acrElement

    Public ElementType As Integer

    Public ElementText As String

    Public ElementIndex As Integer

    Public AssessmentID As Integer

    Public ElementExpandedText As String

    End Structure

    Thanks,

    Adrian.

    Saturday, January 24, 2009 7:07 AM
  • User1868789274 posted

    YOu have declared a structure and not a class. you need to do something like this:
    <Serializable()> _

    Public Class acrElement(Of T)

    Implements IList(Of T),

    private _ElementText

    Public Property ElementText as String

    Get

    Return _ElementText

    End Get

    Set (value as String)

    _Element Text= value

    End Set

    End Class

     

    now this will work:

    Dim acrlist as IList(Of T)

    For i = 0 To acrlist.Count - 1
    acrlist(i).ElementText =
    "hello"
    Next

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 24, 2009 10:50 AM