Reputation: 311
I'm doing one method for calculating a pause, which has certain breaks checks. The method of calculating the time (from-to), at the same time I want to check, and if the time is less than 30, let me report the error.
It would have succeeded in a commencement if the loop would not have been repeated.
Can I somehow, if the condition is correct, skip that part?
In the pause of the variables I come to my values: 6, 56, 4
this is the method
private double CalculateBreak(List<PRAESENZZEIT> arrivals, out string error)
{
error = null;
double totalBreak = 0;
for (int i = 1; i < arrivals.Count();)
{
for (int n=i-1; n < arrivals.Count();)
{
double pause = (arrivals[i].ZPZ_Von - arrivals[n].ZPZ_Bis).TotalMinutes;
//this part
if (pause > 30)
error = "OK";
else
error = "error1";
//end
i += 1;
totalBreak += pause;
break;
}
if (totalBreak < 60)
{
error = "error2";
}
}
return totalBreak;
}
Upvotes: 4
Views: 85
Reputation: 7344
Edit: after some to-and-fro in the comments there is a further change required. If this still doesn't do what you want then lease open another question which concentrates on that part alone because I have no idea what you're saying. Code in the original answer commented out. This now returns the error if there has been one
Your loops are wrong. You only need one of them. The for n loop is going through all arrivals, when it looks like you only need to look at the previous one. Try something like this:
private double CalculateBreak(List<PRAESENZZEIT> arrivals, out string error)
{
error = null;
double totalBreak = 0;
for (int i = 1; i < arrivals.Count();)
{
// Check the i-th against the previous one (i - 1)
double pause = (arrivals[i].ZPZ_Von - arrivals[i - 1].ZPZ_Bis).TotalMinutes;
// //this part
// if (pause > 30)
// error = "OK";
// else
if (pause < 30) // new
{ // new
error = "Pausenzeitverletzung";
} // new
//end
i += 1;
totalBreak += pause;
// break;
// if (totalBreak < 60)
// {
// error = "Pausenzeitverletzung";
// }
}
// new: moved if block
if (totalBreak < 60)
{
error = "Pausenzeitverletzung";
}
return totalBreak;
}
Upvotes: 2