Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

c# - Exception and messagebox

i have a problem with exception handling. Im doing a windows form app to check the validity of social security number. However, when i test it the system takes over. Instead of having the error shown in my MessageBox, it shows directly in the code (debugging mode). Below is Validation, my error checking method and the button in which i call it. Thanks for your help

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! ");
    }
}
question from:https://stackoverflow.com/questions/65646664/exception-and-messagebox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I doubt if we should use any exceptions here, routine (and a bit tedious) checking is enough.

Let's redesign Validation method: we'll return either empty string (for no error) or corresponding error message:

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

Then we can use it:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...