需要判断用户输入的数据并在输入错误的情况下在屏幕上显示通知.我用了下面的方法,但在我看来,它不太符合"不重复自己"的原则.有没有人对简化有什么 idea ?
int productid = 0;
string errorMessage = "Неправильный формат данных:\n",
productName = "", productGroup = "", productType = "";
if (!int.TryParse(ProductIdTB.Text, out productId))
{
errorMessage += "+ Номер продукта\n";
}
if (string.IsNullOrEmpty(ProductNameTB.Text))
{
errorMessage += "+ Название продукта\n";
}
else
{
productName = ProductNameTB.Text;
}
if (string.IsNullOrEmpty(ProductGroupTB.Text))
{
errorMessage += "+ Группа продукта\n";
}
else
{
productGroup = ProductGroupTB.Text;
}
if (string.IsNullOrEmpty(ProductType.Text))
{
errorMessage += "+ Вид продукта";
}
else
{
productType = ProductType.Text;
}
if (errorMessage.Split(' ').Length > 1)
{
MessageBox.Show(errorMessage);
return;
}