Throw an error when the user enter null or empty string

前端 未结 3 1451
情话喂你
情话喂你 2021-01-04 19:50

I\'m attempting to resolve the following exercise:

You need to create a class named Product that represents a product. The class has a

相关标签:
3条回答
  • 2021-01-04 20:09

    Your if state is wrong. Let's do a truth table:

    if (value != String.Empty || value != null)
    

    Name = null   True Or False  = True 
    Name = "name" True Or True = True
    Name = ""     False Or True = True 
    

    Your if statement is always true!

    I would re-write it thus:

    if (value == String.Empty || value == null)
    {
         throw new ArgumentException("Name cannot be null or empty string", "Name");  
    }
    else
    {
         name = value;
    }
    

    you could just change the Or to and AND but I think the above reads better (the below has an unnecessary double negative):

    if (value != String.Empty && value != null)
    {
       name = value;
    }
    else
    {
        throw new ArgumentException("Name cannot be null or empty string", "value");
    }
    

    As Dmitry Bychenko says, I didn't notice you were not testing for value. In getters you should use the value property. Not the name of your property


    The second parameter (again pointed out by Dmitry Bychenko) in your exception should be:

    The name of the parameter that caused the current exception.

    MSDN

    which in your case is the string "value":

    throw new ArgumentException("Name cannot be null or empty string", "value");
    
    0 讨论(0)
  • 2021-01-04 20:20

    Use String.IsNullOrEmpty Method (String). Change your set like this:

    set
    {
          if (!string.IsNullOrEmpty(value))
          {
                name = value;
          }
          else
          {
                throw new ArgumentException("Name cannot be null or empty string", "value");
          }
    }
    

    Also you can use String.IsNullOrWhiteSpace Method (String) that Indicates whether a specified string is null, empty, or consists only of white-space characters.

    0 讨论(0)
  • 2021-01-04 20:23

    If you want different exceptions on null and on empty string (often null means that something is totally wrong, when empty string is just a format error):

    public string Name {
      get {
        return name;
      }
      set {
        if (null == value)
          throw new AgrumentNullException("value");
        else if (String.Equals(value, ""))
          throw new AgrumentException("Empty values are not allowed.", "value");
    
        name = value; 
      }
    }
    

    In case you don't want to distiguish them:

    public string Name {
      get {
        return name;
      }
      set {
        if (String.IsNullOrEmpty(value))
          throw new AgrumentException("Null or empty values are not allowed.", "value");
    
        name = value; 
      }
    }
    

    Note, that in both cases it's value that you have to test, not a property Name. In your original code the name's (and so Name as well) initial value is null and you'll get exception whatever you try to set.

    0 讨论(0)
提交回复
热议问题