如何使用C#在ASP.NET中使用IF语句 [英] How to use IF statement in asp.net using C#

查看:144
本文介绍了如何使用C#在ASP.NET中使用IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦所有这些条件都成立,我将尝试发送电子邮件,但是我的问题是,即使条件设置为false,它仍在发送电子邮件.例如,如果var3或var4或var5为否",则我不想发送电子邮件.这是我正在检查的伪代码,为了将电子邮件发送出去,它们都必须是真实的:

I am trying to send an email once all these conditions are found to be true but my problem is that it is still sending an email out even if the condition is set to false. For example, if either var3 or var4 or var5 is "NO" then I don’t want to send email out. Here is the pseudo code for what I am checking and they all have to be true in order to send the email out:

If(var1 is not blank And var2 is not set to "YES" And (either var3 OR var4 OR var5 are not set to "NO")
Then 
{
Send_Email();
}

这是我当前的代码:

if var1 != (" ") && var2!= "YES" && var3!= ("NO") || var4!= ("NO") || var5!= ("NO") ))
{
sendEmail();
}

推荐答案

由于您的条件很大,因此没有什么可以阻止您编写多个小的方法来增强可读性.

Since your conditions are pretty big, nothing stops you from writing multiple small methods to enhance readability.

此外,您应该使用 string.IsNullOrEmpty .而不是检查!=& nbsp" .

Also, instead of checking for != "&nbsp", you should use string.IsNullOrEmpty.

private bool IsVar1Blank(string var1)
{
    return string.IsNullOrWhiteSpace(var1);
}

private bool IsVar2SetToYes(string var2)
{
    return var2 == "YES";
}

private bool IsAnOtherVariableNotSetToNo(string var3, string var4, string var5)
{
    return var3 != ("NO") || var4 != ("NO") || var5 != ("NO");
}

您将得到以下结果:

if (!IsVar1Blank(var1) && !IsVar2SetToYes(var2) && IsAnOtherVariableNotSetToNo(var3, var4, var5)
{
    sendEmail();
}

此外,为了始终确保您的代码有效,您应该真正考虑编写单元测试.

Also, to always be sure your code works, you should really look into writing Unit Tests.

这篇关于如何使用C#在ASP.NET中使用IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆