Consider the following requirement. A Period can have anywhere from zero to forty eight Activities (though most will have only three) and each Activity within a Period should have a unique, non-sequential, two-digit identifier (e.g. 02, 41, 99).
Now consider how you might implement the code that generates a new identifier. Go ahead, think about it. I'll wait.
Got it? Good. Without even seeing your code, I can all but guarantee that it does not look anything like the code Rafael V's predecessor developed. Rafael found it when researching a customer ticket that simply read "something weird happens when we add 25... 22... or around there Activities; it's not consistent, and doesn't always happen at the same number."
public string GenerateNewActivityCode(Period period) { Random rand = new Random(); bool firstinuse = false; string firstcode = rand.Next(99).ToString(); bool secondinuse = false; string secondcode = rand.Next(99).ToString(); bool thirdinuse = false; string thirdcode = rand.Next(99).ToString(); foreach (Activity activity in period.Activities) { if (activity.Cod.Equals(firstcode)) firstinuse = true; if (activity.Cod.Equals(secondcode)) secondinuse = true; if (activity.Cod.Equals(thirdcode)) thirdinuse = true; } if (!firstinuse) return firstcode; if (!secondinuse) return secondcode; if (!thirdinuse) return thirdcode; return rand.Next(99).ToString(); }
Well, third time was supposed to be the charm.