Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my code i am using MasterLst of List<keyvaluepair><int,>>> type of list.

When i add a TempLst of List<timespansent> type to MasterLst and then when i Clear the TempLst the MasterLst Also get Clear.
How can i Avoid this

Code:

C#
static List<keyvaluepair><int,>>> Masterlst = new List<keyvaluepair><int,>>>();

List<timespansent> lsttemp = new List<timespansent>();


for (int i = 0; i < lstday.Count; i++)
{
lsttemp.Add(lstday[i]);
}
Masterlst.Add(new KeyValuePair<int,>>(DayID, lsttemp));

                          lsttemp.Clear();

Please Help me...
Thanks in Advance....
Posted
Updated 25-May-12 3:11am
v2
Comments
Jim Jos 25-May-12 8:50am    
List<int,>>> Could you explain wat type of generic is it? are you missing two more declarations? lsttemp is added to the master list as reference not as copy!! so when you clear lsttemp masterlst value will also be cleared

1 solution

The problem u've is that lsttemp it's reference is added to Masterlst, in other words the Masterlst lsttemps version is the same as lsttemps seperate list declaration.

If u want to keep track of the list in masterlst u'll need a 2nd list which u place in masterlst.

But if i understand your example correctly u only want to clear lsttemps cause u've added it to the masterlst, isn't it?

If so just don't clear lsttemp set it's value to null instead, so the only reference keeper remaining is masterlst, when u remove the list from the masterlist u could decide to clear it then, or not at all if u want the garbagecollector to take care of it all.

I hope i've supplied enough information.
 
Share this answer
 
Comments
curiouspoo 26-May-12 2:04am    
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900