Array Lists

Discussion in 'ASP .Net' started by unwantedspam, Nov 29, 2005.

  1. unwantedspam

    unwantedspam 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
     
    unwantedspam, Nov 29, 2005
    #1
    1. Advertising

  2. 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)

    "unwantedspam" <> wrote in message
    news:...
    > 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
    >
     
    Ganesh Ramamurthy, Nov 29, 2005
    #2
    1. Advertising

  3. 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

    "unwantedspam" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?dGlta2xpbmc=?=, Nov 29, 2005
    #3
  4. 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.

    "unwantedspam" <> wrote in message
    news:...
    > 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
    >
     
    Peter Rilling, Nov 29, 2005
    #4
  5. unwantedspam

    Bruce Barker Guest

    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)



    "unwantedspam" <> wrote in message
    news:...
    > 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
    >
     
    Bruce Barker, Nov 29, 2005
    #5
  6. unwantedspam

    unwantedspam Guest

    Bruce Barker wrote:
    > 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)
    >
    >
    >
    > "unwantedspam" <> wrote in message
    > news:...
    > > 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
    > >




    Thank you all for you quick and accurate solutions. I greatly
    appreciate it.
     
    unwantedspam, Nov 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    334
    JustSomeGuy
    Jun 17, 2004
  2. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    435
    Buster
    Dec 13, 2004
  3. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    411
    Charlotte Henkle
    Sep 26, 2004
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. bahoo
    Replies:
    3
    Views:
    316
    Bruno Desthuilliers
    Apr 3, 2007
Loading...

Share This Page