【问题标题】:Exception and messagebox异常和消息框
【发布时间】:2021-01-09 19:18:23
【问题描述】:

我遇到了异常处理问题。我正在做一个 Windows 窗体应用程序来检查社会安全号码的有效性。但是,当我对其进行测试时,系统会接管。 它没有在我的 MessageBox 中显示错误,而是直接显示在代码中(调试模式)。 下面是验证,我的错误检查方法和我调用它的按钮。 感谢您的帮助

private bool Validation(String s, out string message) 
{
    message = "";
    bool res = true;
    if (isNumeric(s) == false)
    {
        message = "Valeur non numérique";
        res = false;
    }
    else if (s.Length > 15)
    {
        message = "Vous avez entré trop de caractères";
        res = false;
    }

    if (s.Length < 15)
    {
        message = "Vous n'avez pas entré assez de caractères";
        res = false;
    }

    int genre = Convert.ToInt32(s.Substring(0, 1));
    if (genre != 1 || genre != 2)
    {
        message = "Genre inconnu";
        res = false;
    }

    int mois = Convert.ToInt32(s.Substring(3, 2));
    if (mois < 1 || mois > 12)
    {
        message = "Le mois entré est incorrect";
        res = false;
    }

    long r = Convert.ToInt64(s.Substring(0, 13));
    int clef = ((int)(97 - (r % 97)));
    int r1 = Convert.ToInt32(s.Substring(13, 2));
    if (clef != r1)
    {
        message = "La clef est incorrecte";
        res = false;
    }
    else
    {
        res = true;
        TxtNumSS.Clear();
    }

    return res;
}

private void btn_Click(object sender, EventArgs e)
{
    string message = string.Empty;
    if( Validation(TxtNumSS.Text,out message) == false)
    {
        MessageBox.Show("error", message, MessageBoxButtons.YesNo);
    }
    else
    {
        MessageBox.Show("Clef valide! ");
    }
}

【问题讨论】:

  • 好的格式是你的朋友 Licuen,使用它。 stackoverflow.com/editing-help
  • 您还应该阅读long.TryParseint.TryParse。不确定您所在国家/地区的格式,但对于美国(或加拿大)号码,一个好的算法是采用输入字符串,删除分隔符 (input.Replace("-", string.Empty))。然后使用int.TryParse 来测试它是否是一个有效数字(也许是测试范围)。最后,使用自定义格式字符串将其转换回格式正确的字符串(前导零、分隔符等)

标签: c# visual-studio exception


【解决方案1】:

我怀疑我们是否应该在这里使用任何异常,例行(而且有点乏味)检查就足够了。

让我们重新设计Validation 方法:我们将返回空字符串(无错误)或相应的错误消息:

using System.Linq;

...

private static string ValidationMessage(string s) {
  if (null == s)
    return "null string";
  if (s.Length > 15)
    return "Vous avez entré trop de caractères";
  if (s.Length < 15)
    return "Vous n'avez pas entré assez de caractères";
  if (!s.All(c => c >= '0' && c <= '9')) // all characters in s are [0..9] digits
    return "Valeur non numérique";
  if (s[0] != '1' && s[0] != '2')
    return "Genre inconnu";

  int mois = int.Parse(s.Substring(3, 2));
  
  if (mois < 1 || mois > 12)
    return "Le mois entré est incorrect"; 

  long clef = 97 - long.Parse(s.Substring(0, 13)) % 97;
  long r2 = long.Parse(s.Substring(13, 2));

  if (r2 != clef)
    return "La clef est incorrecte";

  return "";
}

那么我们就可以使用它了:

private void btn_Click(object sender, EventArgs e) {
  string errorMessage = ValidationMessage(TxtNumSS.Text);

  if (string.IsNullOrEmpty(errorMessage)) {
    //TODO: check: should we clear TxtNumSS here?
    TxtNumSS.Clear();
    MessageBox.Show("Clef valide! ");
  }
  else 
    MessageBox.Show("error", errorMessage, MessageBoxButtons.YesNo);  
}

【讨论】:

  • 谢谢你,让我看到了我的错误。
猜你喜欢
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2015-11-18
  • 2018-10-29
  • 2021-03-19
  • 2010-12-15
相关资源
最近更新 更多