[英]Validate user input with multiple criteria c#
I need to validate input based on whether or not it's a number and whether or not it exceeds inventory levels. 我需要根据是否为数字以及是否超出库存水平来验证输入。
So far I can check for inventory level, but I only know how to use tryparse to check if its a number, and I don't want to output anything. 到目前为止,我可以检查库存水平,但是我只知道如何使用tryparse来检查其是否为数字,并且我不想输出任何内容。 Here's what I have so far.
到目前为止,这就是我所拥有的。 It gives an error because I didn't give it a variable.
因为我没有给它一个变量,所以它给出了一个错误。
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");
}
}
I want it to allow me to continue with the code if the input is appropriate and show a message box if it's not. 我希望它允许我在输入正确的情况下继续执行代码,在不合适的情况下显示一个消息框。
It gives an error because I didn't give it a variable
它给出了一个错误,因为我没有给它一个变量
Then give it one: 然后给它一个:
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.