views:

345

answers:

3

hey, this i have a loop that adds gathered strings and ints into an object, and then adds that object into a list. I need it to check if there is already an object with a property == to the one i'm about to assign, and then depending on which the property is, I either want to change one of its properties, or still add a new object. I'll be sorting this later. Here's where I'm stuck. I have marked the line (currently commented out) which when activated, causes the command line to freeze. The weird thing is that i use the IDENTICAL code earlier with no trouble.

As you can see, I have some other code currently commented out, as this bit needs to work before I can continue, but I left it in to allow you to get more of an idea of what i am doing.

solved. i actually had to move it another two loops upwards. THANKS!

     //IF THIS IS THE FIRST ONE, ADD IT!
  var refSize = Referrals.Count();
  if (refSize == 0)
  {
  var NewReferral = new Referral(referringURL.Trim(), referringWords.Trim(), 3);
  Referrals.Add(NewReferral);
  }
  else
  {
  for (int i=0;i<refSize;i++)
  {

   // RESET BOOLS
   URLPresent = false;
   KeywordPresent = false;

   // IF THE URL IS ALREADY PRESENT
   //if (Referrals[i].URL == referringURL)
   //{
    //URLPresent = true;

    // CHECK IF THE KEYWORD IS ALREADY PRESENT
    //for (int ii=0;ii<Referrals[i].Keywords.Count;ii++)
    //{
    // if (Referrals[i].Keywords[ii] == referringWords)
    // {

      // ADD TO OCCURRENCES
    //  Referrals[i].Occurrences++;
    //  KeywordPresent = true;
    // }
    //}

    // ADD KEYWORD TO LIST
    // ###
    // ###
   //}

   // IF THE KEYWORD ISN'T THERE && THE URL ISNT THERE, ADD A NEW REFERRAL OBJECT
   if (URLPresent == false && KeywordPresent == false)
   {
    var NewReferral = new Referral(referringURL.Trim(), referringWords.Trim(), 3);
    //Referrals.Add(NewReferral);  //HERE IS MY PROBLEM! UNCOMMENTING THIS LINE CAUSES A FAIL.

    //URLPresent = true;
    //KeywordPresent = true;
   }

   // IF THE URL IS THERE, BUT THE KEYWORD ISNT, ADD AN ELEMENT TO THE REFERRAL.KEYWORDS LIST
   //else if (URLPresent == true && KeywordPresent == false)
   //{
    //Referrals[i].Keywords.Add(referringWords);

    //URLPresent = true;
    //KeywordPresent = true;
   //}

  }
  }
+5  A: 

The specific problem is that you're chasing your own tail. Every time you add a new element to the list, Refferals.Count goes up (your loop will never end if you keep adding elements). You should either pull the count out into a variable and use that into your loop or use something like Referrals.Contains ...

var refSize = Refferals.Count();

for (int i=0;i < refSize; i++)
{
 // same
}
JP Alioto
+1 beat me by 10 seconds
Stan R.
I had to test it in LiNQPad to make sure I wasn't going crazy. :)
JP Alioto
thanks for pointing me to LiNQPad, first time I am seeing it and it's looks amazing.
Stan R.
You're welcome -- it's a wonderful tool! Make sure you check out the Sample called "The Big Dump" :)
JP Alioto
A: 

Every time you add a Referral object to your referral list you are causing the loop to take one extra iteration. If you keep adding Referrals on every iteration the loop will run indefinately.

Try putting a breakpoint on the line that you commented out. It should be hit repeatedly. The freezing you are experiencing is caused by your infinite loop.

Robert Venables
+1  A: 

The problem is that after you add it to the list, the loop runs through the newly added item and adds it again (probably due to a mistake you made somewhere), which repeats forever.

You can step through your code in the debugger and see.

As an aside, you shouldn't be calling the Count() method in the loop. Instead, use the Count property (without parentheses)

SLaks