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
Second Iteration
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
Second Iteration
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 |=
:
bool successCheckPoint = false;
bool failureCheckPoint = 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");
}
}
a |= b;
is short for a = a | b;
. So if a
is already true, it stays true. If a
is false and b
is true, then a
becomes true. Otherwise, a
remains false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With