Problem with reference I think

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arjen

    Problem with reference I think

    Hi,

    I have a public class 'settings' with a public static void
    'loadsettings'.

    Inside 'loadsettings' I go to public static 'loadpagesettin gs'. Inside
    this function I have a problem.

    1. I create an arraylist
    2. Load the arraylist with items from the cache
    3. If there are no items in the cache, then run some code (put it in
    the arraylist) and add the arraylist to the cache
    4. // So far it works
    5. Now I remove some items from the arraylist
    6. // Here comes the problem
    7. Now the arraylist in the cache is also changed, but I have added it
    in the cache some lines before!
    8. I tried to make an copy of the arraylist, i.e. arraylist2 =
    arraylist, but this does not work

    I need to change that variable after adding it to the cache without
    changing the cache.
    Can somebody help me with this?

    Thanks!

  • Jon Skeet [C# MVP]

    #2
    Re: Problem with reference I think

    Arjen <boah123@hotmai l.comwrote:
    I have a public class 'settings' with a public static void
    'loadsettings'.
    >
    Inside 'loadsettings' I go to public static 'loadpagesettin gs'. Inside
    this function I have a problem.
    >
    1. I create an arraylist
    2. Load the arraylist with items from the cache
    3. If there are no items in the cache, then run some code (put it in
    the arraylist) and add the arraylist to the cache
    4. // So far it works
    5. Now I remove some items from the arraylist
    6. // Here comes the problem
    7. Now the arraylist in the cache is also changed, but I have added it
    in the cache some lines before!
    8. I tried to make an copy of the arraylist, i.e. arraylist2 =
    arraylist, but this does not work
    Well, it does exactly what it's supposed to do, which is to create a
    copy of the *reference*. That's not copying the arraylist itself. If
    you want to create a copy of the arraylist, use ArrayList.Clone .
    I need to change that variable after adding it to the cache without
    changing the cache.
    Can somebody help me with this?
    It's very important that you understand how reference types work. See
    http://www.pobox.com/~skeet/csharp/parameters.html and
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    for more on this.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Arjen

      #3
      Re: Problem with reference I think

      Prrrr, I tried a lot of things... but can't find a solution. ;-(

      ArrayList.Clone seems not to work. I don't know why.

      Other problem is that I have to delete items from the arraylist inside
      a loop. When I use foreach then I can not delete items because I am in
      the loop... suggestion?

      On paper it looks so easy.....

      Arjen




      Jon wrote:
      Arjen <boah123@hotmai l.comwrote:
      I have a public class 'settings' with a public static void
      'loadsettings'.

      Inside 'loadsettings' I go to public static 'loadpagesettin gs'. Inside
      this function I have a problem.

      1. I create an arraylist
      2. Load the arraylist with items from the cache
      3. If there are no items in the cache, then run some code (put it in
      the arraylist) and add the arraylist to the cache
      4. // So far it works
      5. Now I remove some items from the arraylist
      6. // Here comes the problem
      7. Now the arraylist in the cache is also changed, but I have added it
      in the cache some lines before!
      8. I tried to make an copy of the arraylist, i.e. arraylist2 =
      arraylist, but this does not work
      >
      Well, it does exactly what it's supposed to do, which is to create a
      copy of the *reference*. That's not copying the arraylist itself. If
      you want to create a copy of the arraylist, use ArrayList.Clone .
      >
      I need to change that variable after adding it to the cache without
      changing the cache.
      Can somebody help me with this?
      >
      It's very important that you understand how reference types work. See
      http://www.pobox.com/~skeet/csharp/parameters.html and
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      for more on this.
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Problem with reference I think

        Arjen <boah123@hotmai l.comwrote:
        Prrrr, I tried a lot of things... but can't find a solution. ;-(
        >
        ArrayList.Clone seems not to work. I don't know why.
        Well, we can't possibly say what's wrong without more code.

        Could you post a short but complete program which demonstrates the
        problem?

        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.
        Other problem is that I have to delete items from the arraylist inside
        a loop. When I use foreach then I can not delete items because I am in
        the loop... suggestion?
        >
        On paper it looks so easy.....
        One common way is to create a new list of items to delete, then delete
        them outside the loop.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...