私は最初に次のコードを持っていました:
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;
}
}
しかし、foreachのその後の反復では、successCheckPointまたはfailureCheckPointブール値がtrueに設定されていても、割り当ての設定方法が原因でfalseに設定されることになります。
問題の例
最初の反復
- timeFoundはtrueです
- successCheckPointはfalseです
- row.Textには必要なテキストが含まれています
- successCheckPointは確かにfalseです
- successCheckPointをtrueに設定
2回目の反復
- timeFoundはtrueです
- successCheckPointはtrueです
- row.Textに必要なテキストが含まれていません
- successCheckPointはfalseではありません
- 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;
}
}
}
これは私が望んでいることを実行しますが、このタイプの動作を実現するためのより良い方法があるはずだと感じています。ブール値がtrueに設定されると、将来の反復でfalseに戻らないように設定する方法はありますか?
正しい動作
最初の反復
- timeFoundはtrueです
- successCheckPointはfalseです
- row.Textには必要なテキストが含まれています
- successCheckPointは確かにfalseです
- successCheckPointをtrueに設定
2回目の反復
- timeFoundはtrueです
- successCheckPointはtrueなので、再評価をスキップします
それでも混乱する場合は申し訳ありません。必要に応じてもう少し説明できます。
編集:今私はそれについて考えるので、私は本当に'を必要としませんか?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");
}
}
}
みんな助けてくれてありがとう!これが私が決めたコードのバージョンです:
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;
}
}