By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

Problem with reference I think

P: n/a
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!

Sep 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Arjen <bo*****@hotmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 25 '06 #2

P: n/a
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 <bo*****@hotmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 28 '06 #3

P: n/a
Arjen <bo*****@hotmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.