Solved

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

Posted on 2014-07-25
2
279 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

728 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