I initially had the following code:
Boolean successCheckPoint = false;
Boolean failureCheckPoint = false;
Boolean timeFound = false;
foreach (var row in auditRows)
{
timeFound = row.Text.Contains(sCurrentTime) || row.Text.Contains(sLenientTime) || row.Text.Contains(sLenientTime2) ? true : false;
if (timeFound)
{
successCheckPoint = row.Text.Contains("Web User Login Success") && !successCheckPoint ? true : false;
failureCheckPoint = row.Text.Contains("Web User Login Failure") && !failureCheckPoint ? true : false;
}
}
But I found that in later iterations of the foreach, even if the successCheckPoint or failureCheckPoint booleans had been set to true, they’d end up getting set to false because of the way that I’d set up the assignment.
Example problem
First Iteration
- timeFound is true
- successCheckPoint is false
- row.Text does contain the text I want
- successCheckPoint is indeed false
- successCheckPoint set to true
Second Iteration
- timeFound is true
- successCheckPoint is true
- row.Text does not contain the text I want
- successCheckPoint is not false
- successCheckPoint set to false
So to fix the problem, I changed the code into this:
Boolean successCheckPoint = false;
Boolean failureCheckPoint = false;
Boolean timeFound = false;
foreach (var row in auditRows)
{
timeFound = row.Text.Contains(sCurrentTime) || row.Text.Contains(sLenientTime) || row.Text.Contains(sLenientTime2) ? true : false;
if (timeFound)
{
if (!successCheckPoint)
{
successCheckPoint = row.Text.Contains("Web User Login Success") ? true : false;
}
if (!failureCheckPoint)
{
failureCheckPoint = row.Text.Contains("Web User Login Failure") ? true : false;
}
}
}
This does what I want, but it feels like there should be a better way to go about accomplishing this type of behavior. Is there any way to set things up so that once a boolean is set to true, it won’t get changed back to false for future iterations?
Correct Behavior
First Iteration
- timeFound is true
- successCheckPoint is false
- row.Text does contain the text I want
- successCheckPoint is indeed false
- successCheckPoint set to true
Second Iteration
- timeFound is true
- successCheckPoint is true so skip re-evaluation
Sorry if this is still confusing. I can explain a little bit more if necessary.
Edit: Now that I think about it I don’t really need the ‘? true : false’ parts for this code.
New Code:
Boolean successCheckPoint = false;
Boolean failureCheckPoint = false;
Boolean timeFound = false;
foreach (var row in auditRows)
{
timeFound = row.Text.Contains(sCurrentTime) || row.Text.Contains(sLenientTime) || row.Text.Contains(sLenientTime2);
if (timeFound)
{
if (!successCheckPoint)
{
successCheckPoint = row.Text.Contains("Web User Login Success");
}
if (!failureCheckPoint)
{
failureCheckPoint = row.Text.Contains("Web User Login Failure");
}
}
}
Thanks for the help everyone! Here’s the version of the code I’ve settled on:
Boolean successCheckPoint = false;
Boolean failureCheckPoint = false;
Boolean timeFound = false;
foreach (var row in auditRows)
{
if (row.Text.Contains(sCurrentTime) || row.Text.Contains(sLenientTime) || row.Text.Contains(sLenientTime2))
{
successCheckPoint |= row.Text.Contains("Web User Login Success");
failureCheckPoint |= row.Text.Contains("Web User Login Failure");
}
if (successCheckPoint && failureCheckPoint)
{
break;
}
}
You can use the OR assignment operator
|=:a |= b;is short fora = a | b;. So ifais already true, it stays true. Ifais false andbis true, thenabecomes true. Otherwise,aremains false.