Simple question on updating List items
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Simple question on updating List items

  1. #1
    Join Date
    Jul 2014
    Posts
    2

    Simple question on updating List items

    I'm trying to find a simple way to update the items in a list. When using For...Each the items do not actually get updated. See the following code...

    Private Structure test
    Dim iNum As Integer
    Dim strStr As String
    End Structure
    Private oTest As New List(Of test)


    Dim newTest = New test
    newTest.iNum = 1
    newTest.strStr = "a"
    oTest.Add(newTest)
    For Each testItem As test In oTest
    testItem.iNum = 2
    Next

    The list hasn't updated at this point. How can the list be modified?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Simple question on updating List items

    What's the problem? What kind of list?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2014
    Posts
    2

    Re: Simple question on updating List items

    The problem is that while iterating through a generic list, updating the iterated member does not actually update the list value itself. Please see the example code.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Simple question on updating List items

    Something BACKWARDS is what you want.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
       Dim s%, x As Integer
       s = List1.TopIndex
       If s < List1.ListCount - 6 Then
         List1.TopIndex = List1.TopIndex + 5
       Else
         List1.TopIndex = 0
       End If
       For x = 0 To List1.ListCount - 1
     ' Reverse the parms
        If List1.Selected(x) = True Then
          List1.Selected(x) = False
        End If
       Next x
    End Sub
    
    Private Sub Form_Load()
    Dim x%
    For x = 0 To 15
      List1.AddItem x + 1
    Next x
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center