如何使布尔表现得像'闩锁'?

Mic*_*sta 4 c# boolean

我最初有以下代码:

        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;
            }                                

        }
Run Code Online (Sandbox Code Playgroud)

但是我发现在foreach的后续迭代中,即使successCheckPoint或failureCheckPoint布尔值设置为true,由于我设置赋值的方式,它们最终会被设置为false.


示例问题

第一次迭代

  1. timeFound是真的
  2. successCheckPoint是假的
  3. row.Text包含我想要的文本
  4. successCheckPoint确实是假的
  5. successCheckPoint设置为true

第二次迭代

  1. timeFound是真的
  2. successCheckPoint是真的
  3. row.Text不包含我想要的文本
  4. successCheckPoint不是假的
  5. successCheckPoint设置为false

所以为了解决这个问题,我将代码更改为:

        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;
                }
            }                                

        }
Run Code Online (Sandbox Code Playgroud)

这就是我想要的,但感觉应该有更好的方法来完成这种行为.有没有办法设置,以便一旦布尔值设置为true,它将不会被更改回false为未来的迭代?


正确的行为

第一次迭代

  1. timeFound是真的
  2. successCheckPoint是假的
  3. row.Text包含我想要的文本
  4. successCheckPoint确实是假的
  5. successCheckPoint设置为true

第二次迭代

  1. timeFound是真的
  2. successCheckPoint是真的,所以跳过重新评估

对不起,如果这仍然令人困惑.如有必要,我可以解释一下.


编辑:现在我想起来,我真的不需要'?true:false'此代码的部分.

新守则:

        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");
                }
            }                                

        }
Run Code Online (Sandbox Code Playgroud)

感谢大家的帮助!这是我已经确定的代码版本:

        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;
            }

        }
Run Code Online (Sandbox Code Playgroud)

dtb*_*dtb 8

您可以使用OR赋值运算符|=:

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");
    }                                
}
Run Code Online (Sandbox Code Playgroud)

a |= b;是的缩写a = a | b;.所以,如果a已经是真的,它仍然是真的.如果a是假的并且b是真的,则a变为真.否则,a仍然是假的.


Ben*_*igt 6

只需使用OR运算符,如:

bool failed = false;

failed |= CheckOne();
failed |= CheckTwo();
Run Code Online (Sandbox Code Playgroud)