Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

A problem with List and clear method:

Dictionary<string, List<DisplayAllQuestionsTable>> tPages= new Dictionary<string, List<DisplayAllQuestionsTable>>();;
List<DisplayAllQuestionsTable> threadsByTopic = new List<DisplayAllQuestionsTable>();
int tPage=0;
        foreach (var topicKeys in postsByTopic)
        {

               if (topicKeys.Key == subTopic)
                {
                    foreach (var item in postsByTopic[topicKeys.Key])
                    {
                        questionNumber++;
                        maximumTopicPages++;
                        threadsByTopic.Add(item);//Adds All DisplayAllTables objects
                        //if there are 20 add a button.
                        if (maximumTopicPages == 20)
                        {
                            tPages.Add(tPage.ToString(), threadsByTopic);//The threadsByTopic clears everytime i call threadsByTopic.clear()

                            threadsByTopic.Clear();
                            tPage++;

                    }
                }

I know that if it is a reference, its reference is passed by value. But if i add it the threadByTopic list to the dictionary..isnt it saved as it is...? or does it have to be reset?

Upvotes: 0

Views: 1241

Answers (1)

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

The instance is added to dictionary, but its still the same list so when u use clear u are clearing the list u just added. U should create new list object instead using clear.

Upvotes: 1

Related Questions