Array Lists

G

Guest

Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you
 
G

Ganesh Ramamurthy

Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)


Dim test1(9) As String

test1(0) = "1"
test1(1) = "2"
test1(2) = "3"
test1(3) = "4"
test1(4) = "5"
test1(5) = "6"
test1(6) = "7"
test1(7) = "8"
test1(8) = "9"
test1(9) = "10"
myList.Add(test1)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox2.Text = myList(1)(4) '= 5 (Correct)
 
G

Guest

The reason being is that you are adding values by reference into an
ArrayList. You have only one reference, you are just changing the values (in
that reference). So whatever you last set the values of the array to be,
that is what all references to that array will give you. His code, if you
missed it, creates a whole new array, so now you have two references to two
different arrays in your ArrayList.

Tim
 
P

Peter Rilling

Because arrays are ref types and you are reusing the same referenced object
for both adds. When you set the values the second time, you are actually
setting the values of the object that is already in the list, then you are
adding the list again. You have two instances to the same object in the
list.
 
B

Bruce Barker

you added the same string array twice to the arraylist.

so mylist(0) and mylist(1) return the same array and will return the same
results.

try:

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

ReDim test(9) 'test points to new array
test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= e
TextBox1.Text = myList(1)(4) '= 5



-- bruce (sqlwork.com)
 
G

Guest

Bruce said:
you added the same string array twice to the arraylist.

so mylist(0) and mylist(1) return the same array and will return the same
results.

try:

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

ReDim test(9) 'test points to new array
test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= e
TextBox1.Text = myList(1)(4) '= 5



-- bruce (sqlwork.com)



Thank you all for you quick and accurate solutions. I greatly
appreciate it.
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top