Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Boolean behave like a 'latch'?

Tags:

c#

boolean

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

  1. timeFound is true
  2. successCheckPoint is false
  3. row.Text does contain the text I want
  4. successCheckPoint is indeed false
  5. successCheckPoint set to true

Second Iteration

  1. timeFound is true
  2. successCheckPoint is true
  3. row.Text does not contain the text I want
  4. successCheckPoint is not false
  5. 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

  1. timeFound is true
  2. successCheckPoint is false
  3. row.Text does contain the text I want
  4. successCheckPoint is indeed false
  5. successCheckPoint set to true

Second Iteration

  1. timeFound is true
  2. 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;
            }

        }
like image 289
Michael Bautista Avatar asked Dec 06 '22 13:12

Michael Bautista


1 Answers

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.

like image 111
dtb Avatar answered Dec 11 '22 08:12

dtb