繁体   English   中英

使用多个条件验证用户输入的C#

[英]Validate user input with multiple criteria c#

我需要根据是否为数字以及是否超出库存水平来验证输入。

到目前为止,我可以检查库存水平,但是我只知道如何使用tryparse来检查其是否为数字,并且我不想输出任何内容。 到目前为止,这就是我所拥有的。 因为我没有给它一个变量,所以它给出了一个错误。

if (ckbSingle.IsChecked.Value)
            {
                if (int.TryParse(txtSingQuan.Text, out Convert.ToInt32(txtSingQuan.Text)))
                if ((singleespresso.DunkinInventory - Convert.ToInt32(txtSingQuan.Text)) <= 0)
                {
                    MessageBox.Show("Espresso low in stock.");

                }
                else
                {
                    ProductList.Add("Single Espresso");
                }

            }

我希望它允许我在输入正确的情况下继续执行代码,在不合适的情况下显示一个消息框。

它给出了一个错误,因为我没有给它一个变量

然后给它一个:

if (ckbSingle.IsChecked.Value)
{
    if (int.TryParse(txtSingQuan.Text, out int qty) && qty >= singleespresso.DunkinInventory)
    {
        // Input is a valid number and is greater than or equals to stock
        ProductList.Add("Single Espresso");

        // qty variable is accessible in that scope if need be
    }
    else
    {
        // Input is either not a number or lower than stock
        MessageBox.Show("Espresso low in stock.");
    }
}

暂无
暂无

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

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