locked
Updating an object updates the object already added in the arraylist RRS feed

  • Question

  • Hi All,

    I am using VS 2008 I am facing a strange issue in one of my class method.

    I have a for loop
    In that for loop I am initiating an object.
    Depending on the lop iteration I set some value of the object.
    Add that object into an arraylist.
    Then set the object as Nothing.

    The issue is when first time loop executes proper value is set in the object and then is added into the arraylist, and then set as nothing.
    Now when the loop executes second time and when the new value is set in the object  and before adding the object to the arraylist if I check the arraylist the value of already added object is also changed as per the second iteration of for loop. I am stucked over here how to go ahead with this.

    Though object is a ref type but If I am initiating the object In for loop and setting the object as nothing at the end of loop still why this is happening I am not sure. Please give let me know what is wrong here.

    Thanks & Regards,
    Girish Nehte

    Wednesday, September 14, 2011 4:07 AM

Answers

  • This works fine in C#, whether I put the definition of the object outside or inside the loop:

    ArrayList aList = new ArrayList();
                                         
    // outside the loop
    Person o;
    for (int j = 0; j < 5; j++)
    {
        o = new Person();
        o.firstName = j.ToString();
        o.lastName = (j+1).ToString();
        aList.Add(o);
        o = null;
    }
    
    //  --or --
    
    // inside the loop
    for (int j = 0; j < 5; j++)
    {
        Person o = new Person();
        o.firstName = j.ToString();
        o.lastName = (j+1).ToString();
        aList.Add(o);
        o = null;
    }
    
    

    I find it hard to believe that it will work in C# but not VB, so perhaps you should post your code.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Monday, September 19, 2011 2:53 AM
    • Marked as answer by Neddy Ren Monday, September 26, 2011 2:16 AM
    Saturday, September 17, 2011 10:38 PM

All replies

  • You'll have to post the relevant code.
    MCP
    Wednesday, September 14, 2011 5:19 AM
  • In the for loop, instantiate your object again. for example,

    For index As Integer = 0 To 10
                Dim emp As New Employee   'You have to do new instantiattion
                'initialize emp object
                '
                employeeList.Add(emp)
                emp = Nothing
    Next
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, September 14, 2011 6:00 AM
  • Hi girish Nehte,

    Welcome to the MSDN Forum.

    I am not sure what your problems are. Could you please show your reproduced code here and show your detailed questions here with one point in one sentence so that we can make sense of your issue.

    Sorry for the misunderstandings.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 16, 2011 8:21 AM
  • This works fine in C#, whether I put the definition of the object outside or inside the loop:

    ArrayList aList = new ArrayList();
                                         
    // outside the loop
    Person o;
    for (int j = 0; j < 5; j++)
    {
        o = new Person();
        o.firstName = j.ToString();
        o.lastName = (j+1).ToString();
        aList.Add(o);
        o = null;
    }
    
    //  --or --
    
    // inside the loop
    for (int j = 0; j < 5; j++)
    {
        Person o = new Person();
        o.firstName = j.ToString();
        o.lastName = (j+1).ToString();
        aList.Add(o);
        o = null;
    }
    
    

    I find it hard to believe that it will work in C# but not VB, so perhaps you should post your code.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Monday, September 19, 2011 2:53 AM
    • Marked as answer by Neddy Ren Monday, September 26, 2011 2:16 AM
    Saturday, September 17, 2011 10:38 PM