简体   繁体   English

c#if或声明指定主题

[英]c# if or statement to specify subject

I am trying to code an if OR statement with an else if to follow up to change an email subject header depending on the RAG (Red Amber Green) status of application checks. 我正在尝试使用else if编写if OR语句,以便根据应用程序检查的RAG(红琥珀色绿色)状态跟进更改电子邮件主题标题。

The if statement checks for the condition of a groupbox BackColor . if语句检查groupbox BackColor的条件。 At the moment the statement only returns Green unless all applications are amber and the subject changes to amber or the same for red. 此语句只返回绿色,除非所有应用程序都是琥珀色,主题变为琥珀色或红色相同。

I am looking for one failure to then change the subject. 我正在寻找一个失败然后改变主题。

The code I have put together is below: 我放在一起的代码如下:

if ((App1.BackColor == Color.Green) | (App2.BackColor == Color.Green) | (App3.BackColor == Color.Green) | (App4.BackColor == Color.Green))
{
    oMailItem.Subject = "Application Start Of Day Status GREEN " + Date;
}

else if ((App1.BackColor == Color.Orange) | (App2.BackColor == Color.Orange) | (App3.BackColor == Color.Orange) | (App4.BackColor == Color.Orange))
{
    oMailItem.Subject = "Application Start Of Day Status AMBER " + Date;
}

else if ((App1.BackColor == Color.Red) | (App2.BackColor == Color.Red) |  (App3.BackColor == Color.Red) | (App4.BackColor == Color.Red))
{
    oMailItem.Subject = "Application Start Of Day Status Red " + Date;
}

I am fairly new to coding in C# using Visual Studio and up to now have found an answer for most things I have had an issue with, but this one I have search both here and on other sites. 我使用Visual Studio在C#中进行编码相当新,到目前为止我已经找到了大多数问题的答案,但是这个我在这里和其他网站都有搜索。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The IF block you have will stop at setting the day status GREEN if any of those colors are green. 如果这些颜色中的任何一种为绿色,那么您所拥有的IF块将停止将日期状态设置为绿色。 Ergo, it will only go into the orange section if none of the items are green and at least one is orange. 如果没有任何项目是绿色且至少一个是橙色,它将只进入橙色部分。 It will only go into the red section if all are red. 如果全部为红色,它将只进入红色部分。

SO 所以

If you want it to fall through the other way, meaning that if ANY are red then emit red you need to do one of two things. 如果你希望它通过另一种方式掉落,意味着如果任何是红色然后发红,你需要做两件事之一。 Either reverse the if statements such that the red ones are at the top OR get rid of the ELSE part and leave them as 3 distinct IF statements. 要么反转if语句,使得红色的语句位于顶部,要么删除ELSE部分,并将它们保留为3个不同的IF语句。

I would reverse the logic so that you check if any of them are RED, then check if any of them are AMBER, and finally if there are no reds or ambers it must be green (depends on your situation of course). 我会反转逻辑,以便检查它们中的任何一个是否为红色,然后检查它们中是否有任何一个是琥珀色,最后如果没有红色或琥珀色它必须是绿色(当然取决于你的情况)。

// If any of them are red, then code red
if ((App1.BackColor == Color.Red) | (App2.BackColor == Color.Red) |  (App3.BackColor == Color.Red) | (App4.BackColor == Color.Red))
{
    oMailItem.Subject = "Application Start Of Day Status Red " + Date;
}
// Otherwise, check if any are amber
else if ((App1.BackColor == Color.Orange) | (App2.BackColor == Color.Orange) | (App3.BackColor == Color.Orange) | (App4.BackColor == Color.Orange))
{
    oMailItem.Subject = "Application Start Of Day Status AMBER " + Date;
}
// Everything's fine so we give it a green
else
    oMailItem.Subject = "Application Start Of Day Status GREEN " + Date;

You're not completely clear with what you want your code to do. 您并不完全清楚您希望代码执行的操作。 If you want everything green unless one is amber, and amber unless one is red, you need to clearly say so. 如果你想要一切都是绿色的,除非一个是琥珀色和琥珀色,除非一个是红色的,你需要清楚地说出来。

If any of the app backcolors are green, then your "subject green" is run and the entire loop terminates. 如果任何app背景颜色为绿色,则运行“主题绿色”并且整个循环终止。 If you want the amber or red condition to change the subject from green to their color, take out the else statements, giving your three separate if statements. 如果您希望琥珀色或红色条件将主题从绿色更改为其颜色,请取出else语句,给出三个单独的if语句。 That way your will get green, then amber if one is amber, then red if one is red. 那样你会变绿,然后琥珀如果是琥珀,那么红色,如果一个是红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM