cycle through an arraylist

spiveygb

New member
Joined
Aug 15, 2006
Messages
2
Programming Experience
Beginner
I have created an object and am adding it to an arraylist. However, when I try to iterate through the arraylist I receive the same value. In the code below I set some properties for the object "objLayerInfo" and then add that to the arraylist "arrLayer." When I try to iterate through the arraylist in using a for...next loop the strLayerID value is always the same. What am I doing wrong? Any help is greatly appreciated.

VB.NET:
For Each node In nodelist
With objLayerInfo
.Visible = node.Attributes.GetNamedItem("visible").InnerText
.Name = node.Attributes.GetNamedItem("name").InnerText
.Id = node.Attributes.GetNamedItem("id").InnerText
If Not IsNothing(node.Attributes.GetNamedItem("minscale")) Then
.MinScale = node.Attributes.GetNamedItem("minscale").InnerText
End If
If Not IsNothing(node.Attributes.GetNamedItem("maxscale")) Then
.MaxScale = node.Attributes.GetNamedItem("maxscale").InnerText
End If
End With
arrlayer.Add(objLayerInfo)
Next
 
Dim i As Integer
Dim alLayers As New ArrayList
Dim objLayer As Object
Dim strLayerID As String
 
For i = 0 To intLayerCount - 1
objLayer = arrlayer.Item(i)
strLayerID = objLayer.Id.ToString()
'strlayerName = objLayer.Name.ToString()
'strlayerVisible = objLayer.Visible.ToString()
Next
 
Last edited by a moderator:
You're adding the same reference (objLayerInfo) to the arraylist, each time you add objLayerInfo it refers to the same object. You must create new objects in the loop to add to the arraylist.
 
Back
Top