Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now