?
Solved

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

Posted on 2014-07-25
2
Medium Priority
?
282 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
[X]
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
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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