473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Changing a Structure that's in an ArrayList

Hi all,

In my VB.NET code below, I try to change the user name in my arraylist from
Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can
anyone explain why this happens and how I might modify a structure in an
arraylist without having to completely remove it and re-add the structure to
the arraylist? I just want to iterate throught the arraylist and change
items based on certain conditions.

Thanks in advance!
Public Structure user
Dim name As String
End Structure

Module Module1

Dim users As ArrayList = New ArrayList
Dim ouser As user

Sub Main()
ouser.name = "Ted"
users.Add(ouser)

For Each ouser In users
ouser.name = "Bob"
Next

users.Add(ouser)

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module

Nov 20 '05 #1
7 2061
Adam,
Remember that a structure is a Value Type, and the ArrayList holds objects,
which means when you add the Structure to an array list a copy is made, as
the Structure is boxed (changed into an object) when you access (Index into
the arraylist) a copy is made. When you use the For Each, a copy is made.

So in the line "user.name = "bob"" you are modifying a copy of the
structure.

To get this to work you need to use an indexed for loop, remove the
structure from the arraylist and put the structure back in, something like:

Dim index As Integer
For index = 0 to users.Count -1 ouser = users(index) ouser.name = "Bob" users(index) = ouser Next
Alternately I would recommend using a Class instead of a Structure, as the
Class is a reference type, which means when you add an instance of the Class
to the ArrayList a copy of the reference is made, when you use a For Each a
copy of the reference is returned, which means you can use a For Each as
expected.

Something like: Public Class user
Dim name As String
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
End Class

Module Module1

Dim users As ArrayList = New ArrayList

Sub Main()
Dim ouser As user
ouser = New User("Ted")
users.Add(ouser)

For Each ouser In users
ouser.name = "Bob"
Next

users.Add(New User("Bill"))

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module

Hope this helps
Jay

"Adam" <no****@comcast.net> wrote in message
news:hgnUb.183280$nt4.783915@attbi_s51... Hi all,

In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can
anyone explain why this happens and how I might modify a structure in an
arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change
items based on certain conditions.

Thanks in advance!
Public Structure user
Dim name As String
End Structure

Module Module1

Dim users As ArrayList = New ArrayList
Dim ouser As user

Sub Main()
ouser.name = "Ted"
users.Add(ouser)

For Each ouser In users
ouser.name = "Bob"
Next

users.Add(ouser)

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module

Nov 20 '05 #2
Cor
In addition to Jay,

Remove that extra add, I was puzzling on it, but never do something with
structures, because I knew that Jay would sure answer this when I did I did
not answer you, because that was not the only problem and I did not want to
give you an alternative but was curious also.

Thanks Jay I think I see it now, I did first send this message before to
check because I did not see that wrong extra add in your message, but that
was not the problem alone.

Cor
Nov 20 '05 #3
Cor
Hi,

This is it (with the structure with an alternative it was no problem)
:-)
\\\
Public Structure user
Dim name As String
End Structure
Module Module1
Dim users As ArrayList = New ArrayList
Dim ouser As user
Sub Main()
ouser.name = "Ted"
users.Add(ouser)
Dim index As Integer
For index = 0 To users.Count - 1
ouser = DirectCast(users(index), user)
ouser.name = "Bob"
users(index) = ouser
Next
' users.Add(ouser)
For Each ouser In users
Console.WriteLine(ouser.name)
Next
Console.ReadLine()
End Sub
End Module
///
Nov 20 '05 #4
Cor
Unreadable sentence
"Cor" <no*@non.com> schreef in bericht
news:eC**************@TK2MSFTNGP12.phx.gbl...
I did first send this message before to

I did not first send this message earlier, to ...........................
Nov 20 '05 #5
See the comments below....

"Adam" <no****@comcast.net> wrote in message
news:hgnUb.183280$nt4.783915@attbi_s51...
Hi all,

In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can
anyone explain why this happens and how I might modify a structure in an
arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change
items based on certain conditions.

Thanks in advance!
Public Structure user
Dim name As String
End Structure

Module Module1

Dim users As ArrayList = New ArrayList
Dim ouser As user

Sub Main()
ouser.name = "Ted"
users.Add(ouser)
For Each ouser In users
ouser.name = "Bob"
Next

The routine above is not actually accessing the objects in the ArrayList
directly. It is simply setting the value of "ouser" to the value of the
current object in the list. Therefore, when you manipulate the data you are
only modifying the copy. The line below then adds the copy into the
ArrayList as a new object.
users.Add(ouser)
To modify the data directly you would need to use a routine similar to the
one below:

Dim i as integer
For i = users.Count - 1 To 0 Step -1
users.Item(i).name = "Bob"
Next

You may not need to iterate the ArrayList in reverse order if you're not
adding or removing items. However, if you do either of these you will need
to go in reverse order or the routine will throw an error because the list
has been modified.

I haven't checked any of this code, but it should work or at least give you
some ideas....

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module

Nov 20 '05 #6
Scott,
users.Item(i).name = "Bob" Did you try this? You get an exception, as you cannot operate on a structure
Late Bound!

If you add a DirectCast to avoid the late binding, you then get an error!.

For details on why it won't work see my response...

Hope this helps
Jay

"Scott" <sc*****@yahoo.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl... See the comments below....

"Adam" <no****@comcast.net> wrote in message
news:hgnUb.183280$nt4.783915@attbi_s51...
Hi all,

In my VB.NET code below, I try to change the user name in my arraylist from
Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and how I might modify a structure in an
arraylist without having to completely remove it and re-add the

structure to
the arraylist? I just want to iterate throught the arraylist and change
items based on certain conditions.

Thanks in advance!
Public Structure user
Dim name As String
End Structure

Module Module1

Dim users As ArrayList = New ArrayList
Dim ouser As user

Sub Main()
ouser.name = "Ted"
users.Add(ouser)
For Each ouser In users
ouser.name = "Bob"
Next


The routine above is not actually accessing the objects in the ArrayList
directly. It is simply setting the value of "ouser" to the value of the
current object in the list. Therefore, when you manipulate the data you

are only modifying the copy. The line below then adds the copy into the
ArrayList as a new object.
users.Add(ouser)
To modify the data directly you would need to use a routine similar to the
one below:

Dim i as integer
For i = users.Count - 1 To 0 Step -1
users.Item(i).name = "Bob"
Next

You may not need to iterate the ArrayList in reverse order if you're not
adding or removing items. However, if you do either of these you will need
to go in reverse order or the routine will throw an error because the list
has been modified.

I haven't checked any of this code, but it should work or at least give

you some ideas....

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module


Nov 20 '05 #7
I did what Cor suggested and it worked great. Thanks all for the help!
Reference types vs. Value types can be trciky at first. Here's what was
suggested and what worked for me:

Hi,

This is it (with the structure with an alternative it was no problem)
:-)
\\\
Public Structure user
Dim name As String
End Structure
Module Module1
Dim users As ArrayList = New ArrayList
Dim ouser As user
Sub Main()
ouser.name = "Ted"
users.Add(ouser)
Dim index As Integer
For index = 0 To users.Count - 1
ouser = DirectCast(users(index), user)
ouser.name = "Bob"
users(index) = ouser
Next
' users.Add(ouser)
For Each ouser In users
Console.WriteLine(ouser.name)
Next
Console.ReadLine()
End Sub
End Module
///

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e$*************@TK2MSFTNGP12.phx.gbl...
Scott,
users.Item(i).name = "Bob" Did you try this? You get an exception, as you cannot operate on a

structure Late Bound!

If you add a DirectCast to avoid the late binding, you then get an error!.

For details on why it won't work see my response...

Hope this helps
Jay

"Scott" <sc*****@yahoo.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
See the comments below....

"Adam" <no****@comcast.net> wrote in message
news:hgnUb.183280$nt4.783915@attbi_s51...
Hi all,

In my VB.NET code below, I try to change the user name in my arraylist

from
Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and how I might modify a structure in an arraylist without having to completely remove it and re-add the structure
to
the arraylist? I just want to iterate throught the arraylist and change items based on certain conditions.

Thanks in advance!
Public Structure user
Dim name As String
End Structure

Module Module1

Dim users As ArrayList = New ArrayList
Dim ouser As user

Sub Main()
ouser.name = "Ted"
users.Add(ouser)

For Each ouser In users
ouser.name = "Bob"
Next


The routine above is not actually accessing the objects in the ArrayList
directly. It is simply setting the value of "ouser" to the value of the
current object in the list. Therefore, when you manipulate the data you

are
only modifying the copy. The line below then adds the copy into the
ArrayList as a new object.
users.Add(ouser)


To modify the data directly you would need to use a routine similar to

the one below:

Dim i as integer
For i = users.Count - 1 To 0 Step -1
users.Item(i).name = "Bob"
Next

You may not need to iterate the ArrayList in reverse order if you're not
adding or removing items. However, if you do either of these you will need to go in reverse order or the routine will throw an error because the list has been modified.

I haven't checked any of this code, but it should work or at least give

you
some ideas....

For Each ouser In users
Console.WriteLine(ouser.name)
Next

Console.ReadLine()
End Sub

End Module



Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
3
by: RBCC | last post by:
I have a form with a listbox and two textboxes. In the listbox I have the make and models of automobiles. and as the user clicks on the make of the car in the listbox I would like to output the make...
2
by: sal | last post by:
Greetings All I'm learning to use arraylist and structures but I'm getting a System.NullReferenceException Error and I'm not sure why. I followed the VB.net book all I did was change the names....
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
8
by: wg | last post by:
I have worked on this for a while and have not been able to get it working. I seem to be missing something. What I am attempting to do is get information from a csv file such as: name (stirng),...
6
by: mm | last post by:
Hi All, I am a newbie in Vb.net and I have some problems I have a structure called padrecord. In this structure i have X,Y len h w name typeval First I add the known values in X and Y. And put...
1
by: =?Utf-8?B?SmF5Qw==?= | last post by:
I am trying to understand how to use an arraylist that contains data in a structure and bind the results to a gridview. Using vs2008 I have looked at the examples 315784 HOW TO: Bind a DataGrid...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.