Solved

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

Posted on 2014-07-25
2
273 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now