473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
End Sub

End Module

Nov 20 '05 #1
7 2073
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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
End Sub

End Module

Hope this helps
Jay

"Adam" <no****@comcast .net> wrote in message
news:hgnUb.1832 80$nt4.783915@a ttbi_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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
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(user s(index), user)
ouser.name = "Bob"
users(index) = ouser
Next
' users.Add(ouser )
For Each ouser In users
Console.WriteLi ne(ouser.name)
Next
Console.ReadLin e()
End Sub
End Module
///
Nov 20 '05 #4
Cor
Unreadable sentence
"Cor" <no*@non.com> schreef in bericht
news:eC******** ******@TK2MSFTN GP12.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.1832 80$nt4.783915@a ttbi_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).n ame = "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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
End Sub

End Module

Nov 20 '05 #6
Scott,
users.Item(i).n ame = "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******** ******@TK2MSFTN GP10.phx.gbl... See the comments below....

"Adam" <no****@comcast .net> wrote in message
news:hgnUb.1832 80$nt4.783915@a ttbi_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).n ame = "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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
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(user s(index), user)
ouser.name = "Bob"
users(index) = ouser
Next
' users.Add(ouser )
For Each ouser In users
Console.WriteLi ne(ouser.name)
Next
Console.ReadLin e()
End Sub
End Module
///

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e$******** *****@TK2MSFTNG P12.phx.gbl...
Scott,
users.Item(i).n ame = "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******** ******@TK2MSFTN GP10.phx.gbl...
See the comments below....

"Adam" <no****@comcast .net> wrote in message
news:hgnUb.1832 80$nt4.783915@a ttbi_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).n ame = "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.WriteLi ne(ouser.name)
Next

Console.ReadLin e()
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
14020
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.) The technical requirements: 1) Store tree data in the database so that it can be extracted as a tree structure. For test purposes,
26
7049
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field...
3
1975
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 and model in the textboxes, how is this done with an arraylist John --- Posted using Wimdows.net NntpNews Component - Posted from .NET's...
2
1100
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. See code below Structure SppElementProperties Dim Element As String Dim North As String Dim South As String
7
6760
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 = LoadOracleData(sql) '____Do amazing things
8
6559
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), unit number (int), address (int), value (int), etc. I created a structre to handle each element. Since there is an unknown number of rows I have used...
6
5199
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 then this structure in a Arraylist Afterwards I want to add the values of len h name and typeval to all the padrecords in the arraylist The count...
1
3262
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 Control to an Array of Objects or Structures by http://support.microsoft.com/?id=315784 316302 HOW TO: Bind a DataGrid Control to an ArrayList of...
5
2471
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 Element 2 - Class Element 1 - ArrayList Of Class Element 1 - Class
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.