-2
foreach (var item in tweets)
{
     XmlNode user = users[i];
     XmlNode text = texts[i];
     XmlNode id = ids[i];
     string userstr = user.InnerText;
     string textstr = text.InnerText;
     string idstr = id.InnerText;
     tweet.user = userstr;
     tweet.text = textstr;
     tweet.id = idstr;
     tweetlist.Add(tweet);
     ++i;

 }

 return tweetlist;

i put a break by the tweetlist.add(tweet) line so i'm sure that it is getting the correct data but everytime it adds a new item to the list it replaces the first one and adds a new one for example

List{id = 1, name = "bob"} + {id = 2, name = "jenny"}

becomes

list {id = 2 name = "jenny"}, {id = 2 , name = "jenny"}
NASSER
  • 5,900
  • 7
  • 38
  • 57

2 Answers2

0

You are modifying a tweet object and then adding it to the list, but you never switch to a different tweet instance before modifying it again in the next loop, so essentially you are adding the same instance to the list again and again (remember: you add object references, not copies!) and just modify that single instance every time.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
0

In the first example you are repeatedly adding a single instance of 'tweet' and then modifying that instance, so all entries in the list will be the same. You need to create a new instance of tweet each time.

PaulF
  • 6,673
  • 2
  • 18
  • 29