Problem with arraylist

N

nondos

I have problem with arraylist the object i get from the array is always the
last i'll give example:

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim array As New ArrayList

Dim classs As New Class1

Dim i As Integer

For i = 1 To 50

classs.a = i

classs.b = i + 1

classs.c = i + 2

array.Add(classs)

Next

For i = 0 To array.Count - 1

Dim test = array.Item(i)

Next

End Sub

End Class

Public Class Class1

Public a As Integer

Public b As Integer

Public c As Integer

End Class

in this example u can see the var' test will always be with 3 var` a,b,c
that their value equel to 50,51,52

any idea?
 
A

Armin Zingler

nondos said:
I have problem with arraylist the object i get from the array is always the
last i'll give example:

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim array As New ArrayList

Dim classs As New Class1

Dim i As Integer

For i = 1 To 50

classs.a = i

classs.b = i + 1

classs.c = i + 2

array.Add(classs)

Next

For i = 0 To array.Count - 1

Dim test = array.Item(i)

Next

End Sub

End Class

Public Class Class1

Public a As Integer

Public b As Integer

Public c As Integer

End Class

in this example u can see the var' test will always be with 3 var` a,b,c
that their value equel to 50,51,52

any idea?

You always add the same object to the arraylist. In the loop you change the
values of this object every time.

Create an object in each loop:

Dim classs As Class1
Dim i As Integer

For i = 1 To 50
classs = New Class1
classs.a = i
classs.b = i + 1
classs.c = i + 2
array.Add(classs)
Next


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top