“A ‘dry’ principle case” 意为“一个‘干燥’原则案例”。

huangapple go评论59阅读模式
英文:

A "dry" principle case

问题

需要检查用户输入的数据,并在输入不正确时在屏幕上显示通知。我使用了以下方法,但我觉得它不太符合"不要重复自己"的原则。有没有人有简化的想法?

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;
}

希望这对你有帮助。

英文:

there is a need to check the data entered by the user and display a notification on the screen in case of incorrect input. I used the following method, but it seems to me that it don't quite fit into the principle of "don't repeat yourself". Does anyone have any ideas for simplification?

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;
}

答案1

得分: 1

我可以想象构建一个执行这些检查并收集所有错误的类,无论是以字符串或字符串列表的形式。

class ErrorMessageBuilder
{
    string totalmessage = "";

    void AppendErrorIfEmpty(TextBox t, string textboxname)
    {
        if (t.Text.IsNullOrEmpty())
        {
            totalmessage += textboxname + " 不能为空" + Environment.NewLine;
        }    
    }

    void AppendErrorIfNotInt(TextBox t, string textboxname)
    {
         int value;
         if (!int.TryParse(t.Text, out value))
         {
             totalmessage += textboxname + " 必须是整数" + Environment.NewLine;
         }
    }
}

这将减少代码量到

var emb = ErrorMessageBuilder();
emb.AppendErrorIfNotInt(ProductIdTB, "产品 ID");
emb.AppendErrorIfEmpty(ProductNameTB, "产品名称");
...

由于这看起来像是 WinForms 开发,您还可以查看 ErrorProvider 类。与工具提示一样,它允许每个控件有一个错误消息。也许这会更加用户友好。

英文:

I can imagine building a class that does the checks for you and collects all errors, either in a string or a list of strings.

class ErrorMessageBuilder
{
    string totalmessage = "";

    void AppendErrorIfEmpty(TextBox t, string textboxname)
    {
        if (t.Text.IsNullOrEmpty())
        {
            totalmessage += textboxname + " can't be empty" + Environment.NewLine;
        }    
    }

    void AppendErrorIfNotInt(TextBox t, string textboxname)
    {
         int value;
         if (!int.TryParse(t.Text, out value))
         {
             totalmessage += textboxname + " must be an integer number"  + Environment.NewLine;
         }
    }
}

This would reduce the code to

var emb = ErrorMessageBuilder();
emb.AppendErrorIfNotInt(ProductIdTB, "Product ID");
emb.AppendErrorIfEmpty(ProductNameTB, "Product Name");
...

Since this looks like WinForms development to me, you can also have a look at the ErrorProvider class. Like with tooltips, it allows one error message per control. Perhaps that would be even more user-friendly.

答案2

得分: 0

根据使用情况,考虑到字符串的不可变性,它可能不会很好地扩展。对于每个 += 操作,您都会创建一个新的字符串实例,字符串越大,占用的内存和垃圾越多...

考虑使用 StringBuilder 以及它的 Append/AppendFormat 方法。

+=/concat, 字符串性能

英文:

Depending on how much this is used, it might not scale out very well considering the immutability of strings. For every += you are creating a new string instance, the bigger the string the more memory and garbage...

Consider StringBuilder and it's Append/AppendFormat methods.

+=/concat, string performance

huangapple
  • 本文由 发表于 2023年6月12日 16:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定