简体   繁体   English

C#if语句始终返回false

[英]C# if statement always returning false

I have an if statement in a web service call that is always returning false. 我在Web服务调用中有一条if语句,该语句始终返回false。 I want it to return an error message if there isn't the words "VERTICALPALLETS", or "PALLETLABELS" in it. 如果其中没有单词“ VERTICALPALLETS”或“ PALLETLABELS”,我希望它返回一条错误消息。 Even if this field has those words in it, it is still returning an error. 即使此字段中包含这些单词,它仍会返回错误。 The query is not the correct one I am using in my application. 该查询不是我在应用程序中使用的正确查询。 Any help is much appreciated. 任何帮助深表感谢。

public bool ValidateDevice(string DeviceID, string sVersion, out string MachineID, out string MachineName, out string Plant, out string Printer1, out string Printer2, out string WrapStation, out string Location, out string sMessage)
{
    MachineID = "";
    MachineName = "";
    Plant = "";
    Printer1 = "";
    Printer2 = "";
    Plant = "";
    WrapStation = "";
    Location = "";
    sMessage = "";


    bool bTest = CheckVersion(sVersion, out sMessage);
    if (bTest == false)
    {
        sMessage = "You do not meet the minimum version requirements. Contact MIS.";
        return false;
    }


    try
    {
        SqlConnection connection = new SqlConnection(capmSADPConnectionString);
        connection.Open();

        string queryString = "select * from DATABASE";
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@DEVICEID", DeviceID);
        command.Parameters.AddWithValue("@APPID", "VTP");

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows == false)
        {
            email myEmail = new email();
            myEmail.SendErrorEmail("No Application Settings for VTP Device ID - " + DeviceID, "VTPError", "VTP Device Settings Error");
            sMessage = "Could not find application settings for this device. Please enter settings.";
            return false;
        }




        reader.Read();

        MachineID = reader.GetValue(0).ToString();
        MachineName = reader.GetValue(1).ToString();
        Plant = reader.GetValue(2).ToString();
        Location = reader.GetValue(3).ToString();
        Printer1 = reader.GetValue(4).ToString();
        Printer2 = reader.GetValue(5).ToString();
        WrapStation = reader.GetValue(6).ToString();

        reader.Close();
        reader = null;
        command = null;
        connection.Close();
        connection = null;

        if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))
        {
            sMessage = "Please enter correct location in Application Setup and try again.";
            return false;
        }



        return true;
    }
    catch (Exception e)
    {
        sMessage = "Unable to retrieve Application Settings for VTP. Please reopen the application.";
        return false;
    }
    return true;
}

The problem here is with the structure of your if statement: 这里的问题在于您的if语句的结构:

if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))
{
    sMessage = "Please enter correct location in Application Setup and try again.";
    return false;
}

You are saying: 你是说:

If the location does not contain 'VERTICALPALLETS'
Or Else
If the location does not contain 'PALLETLABELS'

This will always be true unless the location contains both bits of text! 除非位置包含文本的两位,否则这将始终为真!

You need to change the Or Else to And Also (so || to && ) and therefore your code will work as expected. 您需要将Or Else更改为And Also (所以||更改为&& ),因此您的代码将按预期工作。

Use AND 使用AND

if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))

Alternative solution which is simpler to read. 替代解决方案,更易于阅读。

if (!(Location.ToUpper().Contains("VERTICALPALLETS") || Location.ToUpper().Contains("PALLETLABELS"))

Sounds like the problem is that you'll always be looking for a "false" or "false" condition unless you are looking for a location that contains both VERTICALPALLETS PALLETLABELS 听起来像是问题在于,除非您要查找同时包含VERTICALPALLETS PALLETLABELS的位置,否则您总是会寻找“ false”或“ false”条件

I suggest you try something like 我建议你尝试类似

if (! ( condtion1 || condition2) )
 return message

This line 这条线

if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))

checks whether Location.ToUpper() does not contain "VERTICALPALLETS" or Location.ToUpper() does not contain "PALLETLABELS". 检查Location.ToUpper()不包含“ VERTICALPALLETS”还是Location.ToUpper()不包含“ PALLETLABELS”。 If Location.ToUpper() does not happen to contain both, then the condition is true. 如果Location.ToUpper()不会同时包含两者,则条件为true。 You instead wanted to check whether Location.ToUpper() contains either of them: 相反,您想检查Location.ToUpper()是否包含以下任意一个:

if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))
{
    //...
}

also, it does not makes much sense to calculate ToUpper several times, let's calculate it only once: 同样,多次计算ToUpper并没有多大意义,让我们只计算一次:

String upperLocation = Location.ToUpper();
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))
{
    //...
}

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

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