0

Im trying to Post an unlimited number of likes but looping the cookies and proxies based on the how many cookies are stored in the array. Apparently i++ is unreachable code. What is the reason for that?

public void PostLikes()
{
   PostLike postLike = new PostLike();
   for (int i =0;i<this.cookies.ToArray().Length;i++)
   {
      for (int j = 0; ; j++)
      {
         postLike.PostLike(this.cookies[i], this.importedProxies[i], this.proxyUsernameTextbox, this.proxyPasswordTextbox, this.postsIds[j]);
      }
   }
}
Brian
  • 5,064
  • 7
  • 33
  • 44
user2880751
  • 23
  • 2
  • 7

3 Answers3

7

The real problem is that:

for (int j = 0; ; j++)

Produces an infinite loop, assuming you don't have any other control statements inside (e.g. break, return, goto, throw)

You probably meant to do something like this:

for (int j = 0; j < this.postsIds.Length; j++)
{
    ...
}
p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
2

And don't do this

for (int i =0;i<this.cookies.ToArray().Length;i++)

because

this.cookies.toArray().Length

its evaluating in every iteration of the for loop, you are making 'this.cookies' to array every time so just you get its length? :) You are increasing the complexity of the method

Martin Solev
  • 146
  • 8
  • well it is a list. How do I get the Length of a list? – user2880751 Nov 07 '13 at 20:38
  • @user2880751 see the [`Count` property](http://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx) And if you're curious why lists has `.Count` while arrays have `.Length`, see http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection – p.s.w.g Nov 07 '13 at 20:40
  • @user2880751 Use `list.Count`. Store it in a variable and reference that variable in the loop though so it's only read once. – David Arno Nov 07 '13 at 20:40
  • @DavidArno AFAIK the optimizer is smart enough to not need an extra variable. list.Count is referenced only once if there is no change to list in the loop. – Mr Lister Jan 24 '14 at 15:56
1
for (int j = 0; ; j++)

This is a dead loop, so i++ won't be reached

Matt
  • 5,747
  • 22
  • 36