开发者

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)
        {

     开发者_StackOverflow          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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜