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

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

A "dry" principle case

问题

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

  1. int productid = 0;
  2. string errorMessage = "数据格式不正确:\n",
  3. productName = "", productGroup = "", productType = "";
  4. if (!int.TryParse(ProductIdTB.Text, out productId))
  5. {
  6. errorMessage += "+ 产品编号\n";
  7. }
  8. if (string.IsNullOrEmpty(ProductNameTB.Text))
  9. {
  10. errorMessage += "+ 产品名称\n";
  11. }
  12. else
  13. {
  14. productName = ProductNameTB.Text;
  15. }
  16. if (string.IsNullOrEmpty(ProductGroupTB.Text))
  17. {
  18. errorMessage += "+ 产品组\n";
  19. }
  20. else
  21. {
  22. productGroup = ProductGroupTB.Text;
  23. }
  24. if (string.IsNullOrEmpty(ProductType.Text))
  25. {
  26. errorMessage += "+ 产品类型";
  27. }
  28. else
  29. {
  30. productType = ProductType.Text;
  31. }
  32. if (errorMessage.Split(' ').Length > 1)
  33. {
  34. MessageBox.Show(errorMessage);
  35. return;
  36. }

希望这对你有帮助。

英文:

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?

  1. int productid = 0;
  2. string errorMessage = "Неправильный формат данных:\n",
  3. productName = "", productGroup = "", productType = "";
  4. if (!int.TryParse(ProductIdTB.Text, out productId))
  5. {
  6. errorMessage += "+ Номер продукта\n";
  7. }
  8. if (string.IsNullOrEmpty(ProductNameTB.Text))
  9. {
  10. errorMessage += "+ Название продукта\n";
  11. }
  12. else
  13. {
  14. productName = ProductNameTB.Text;
  15. }
  16. if (string.IsNullOrEmpty(ProductGroupTB.Text))
  17. {
  18. errorMessage += "+ Группа продукта\n";
  19. }
  20. else
  21. {
  22. productGroup = ProductGroupTB.Text;
  23. }
  24. if (string.IsNullOrEmpty(ProductType.Text))
  25. {
  26. errorMessage += "+ Вид продукта";
  27. }
  28. else
  29. {
  30. productType = ProductType.Text;
  31. }
  32. if (errorMessage.Split(' ').Length > 1)
  33. {
  34. MessageBox.Show(errorMessage);
  35. return;
  36. }

答案1

得分: 1

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

  1. class ErrorMessageBuilder
  2. {
  3. string totalmessage = "";
  4. void AppendErrorIfEmpty(TextBox t, string textboxname)
  5. {
  6. if (t.Text.IsNullOrEmpty())
  7. {
  8. totalmessage += textboxname + " 不能为空" + Environment.NewLine;
  9. }
  10. }
  11. void AppendErrorIfNotInt(TextBox t, string textboxname)
  12. {
  13. int value;
  14. if (!int.TryParse(t.Text, out value))
  15. {
  16. totalmessage += textboxname + " 必须是整数" + Environment.NewLine;
  17. }
  18. }
  19. }

这将减少代码量到

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

由于这看起来像是 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.

  1. class ErrorMessageBuilder
  2. {
  3. string totalmessage = "";
  4. void AppendErrorIfEmpty(TextBox t, string textboxname)
  5. {
  6. if (t.Text.IsNullOrEmpty())
  7. {
  8. totalmessage += textboxname + " can't be empty" + Environment.NewLine;
  9. }
  10. }
  11. void AppendErrorIfNotInt(TextBox t, string textboxname)
  12. {
  13. int value;
  14. if (!int.TryParse(t.Text, out value))
  15. {
  16. totalmessage += textboxname + " must be an integer number" + Environment.NewLine;
  17. }
  18. }
  19. }

This would reduce the code to

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

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:

确定