Problem with reference I think

A

Arjen

Hi,

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

Inside 'loadsettings' I go to public static 'loadpagesettings'. 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!
 
J

Jon Skeet [C# MVP]

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

Inside 'loadsettings' I go to public static 'loadpagesettings'. 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
http://www.pobox.com/~skeet/csharp/memory.html
for more on this.
 
A

Arjen

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
 
J

Jon Skeet [C# MVP]

Arjen said:
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.
 

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