2

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);
    }
2
  • 2
    if (string.IsNullOrEmpty(value)) this.reason = "reason not explicited" ...; Commented Apr 28, 2017 at 10:11
  • 2
    Use some basic client side validation instead to display the appropriate validation message. You can also use property attributes if you are using mvc validation. Do not hard code this into your model logic. Commented Apr 28, 2017 at 10:12

7 Answers 7

11

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;
}
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

4

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

Comments

1

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

Comments

0

you need to pass value to the function IsNullOrEmpty function

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

Comments

0

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

3 Comments

Getter should not change values.
Wrong solution.
I wouldn't recommend it. Getter should return actual value.
0

You might do an extension method to IsNullOrEmpty():

public static bool IsNullOrEmpty(this string value) => string.IsNullOrEmpty(str);

And then you can use it this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.