简体   繁体   中英

Check null or empty string

I would like to display 'reason not explicited' if the user doesn't fill this field or the user input if he fills it. With this code, I don't understand why but even if I fill the field, it is going to display 'reason not explicited'.

    private string reason;
    public string Reason
    {
        get
        {
            return this.reason;
        }
        set
        {
            if (string.IsNullOrEmpty(this.Reason))
                this.reason = "reason not explicited";
            else this.reason = value;
        }
    }

    [Pure]
    public static bool IsNullOrEmpty(String value) {
        return (value == null || value.Length == 0);
    }

just use value in your setter to handle the correct behavior

set
{
    if (string.IsNullOrEmpty(value))
        this.reason = "reason not explicited";
    else 
        this.reason = value;
}

Because you are checking the property instead of the value.
Your current code sets the value of the property only if it's null or empty.
It should be

    set
    {
        if (string.IsNullOrEmpty(value))
            this.reason = "reason not explicited";
        else this.reason = value;
    }

Or even better:

    set
    {
        this.reason = (string.IsNullOrEmpty(value)) ? "reason not explicited": value;
    }

You have to check value , not this.Reason :

public string Reason
    {
        get
        {
            return this.reason;
        }
        set
        {
            this.reason = string.IsNullOrEmpty(value)
              ? "reason not explicited"
              : value; 
        }
    }

The problem is in this line of code

if (string.IsNullOrEmpty(this.Reason))

When you are trying to get the value of this property, calls it's getter, which returns value of reason field. And this field is not filled with correct value at the moment you are trying to get value of it.

So you should modify your code to check if null or empty string of concrete input value:

if (string.IsNullOrEmpty(value))

you need to pass value to the function IsNullOrEmpty function

if (string.IsNullOrEmpty(value))
                this.reason = "reason not explicited";
            else this.reason = value;

The setter will trigger when you change(assign) the value, so if the user skip that field means the setter will not trigger. But the getter will trigger when you access the value of the property. So in your case if you apply the logic inside the get means it will display the "reason not explicited" if the reason is null or empty. So the new logic would be like the following:

 get
 {
     if (string.IsNullOrEmpty(this.reason))
         return "reason not explicited";
     else this.reason = value;
         return this.reason;
 }
 set
 {
     this.reason = value;  
 }

Another solution will be like this:

Initialize the backup property with the "reason not explicited" so even if the user skip the field(setter will not trigger) you will get the default value to be printed. And you have to update the backup property based on the value

private string reason = "reason not explicited";

public string Reason
{
    get { return this.reason; }
    set
    {
        if (String.IsNullOrEmpty(value))
            this.reason = "reason not explicited";
        else
            this.reason = value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM