Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Setting an item in a Visual Basic list class when iterating with for each...

Posted on 2014-07-25
2
276 Views
Last Modified: 2014-07-25
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

At this point if I was to query the only item in the list, the value would still be (1, "a"). The list hasn't updated at this point. How can the list be modified?

Thanks
0
Comment
Question by:tolvor
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40220474
Change from a Structure to a Class and it should work.  When you use a Structure a copy is made each time you iterate.  With a Class you will work directly with the item already in the List.
0
 

Author Closing Comment

by:tolvor
ID: 40220554
Brilliant solution. Thank worked. Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question