Question

I have a form with a number of inputs, each of which needs to be Trimmed before they are processed. It would be laborious - and not very robust - to trim these on each use of the value as each is used in numerous places within the form's codebehind.

I'm thinking of doing something like this for each of my properties:

private string _surname
public string Surname
{
    get
    {
        return _surname.Trim();
    }
    set
    {
        _surname = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    Surname = txtSurname.Text;
}

Even this seems pretty verbose, though. Particularly for forms which have a much larger number of inputs. Perhaps simply:

private string _txtSurname;

protected void Page_Load(object sender, EventArgs e)
{
    _txtSurname = txtSurname.Text.Trim();
}

But this doesn't have the same robustness as the first example in that, if I assign to the same property elsewhere, I might forget that I need to trim first. I feel like there should be a much more succinct way of handling this. Is there?

Was it helpful?

Solution

UPDATE: The Extension Method approach, while functional isn't great from a readability perspective. Intuitively, calling foo.NullSafeTrim(); where foo = null will throw a NullReferenceException. However, due to the way it is implemented it doesn't. Meh.

I agree that adding .Trim() to every getter and setter would be laborious. Especially when you have to deal with all of the null checking to avoid NullReferenceExceptions. Another option, which would make the code a bit more compact is to use Extension Methods. For instance:

public static class MyExtensions
{
     public static string NullSafeTrim(this string value)
     {
         if (value != null)
         {
             value = value.Trim();
         }

         return value;
     }
}

Then you could use the following pattern:

private string _surname;
public string Surname
{
    get { return _surname.NullSafeTrim(); }
    set { _surname = value.NullSafeTrim(); }
}

There are some considerations in using extension methods, which you should be able to turn up with a search on SO.

Alternatively, you could do all of the trimming and null checking in whichever class is processing the values.

OTHER TIPS

The empty get won't work, and your setting actually did not SET to the backing field!

private string _txtSurname
{
   get;  // problematic!
   set { return value.Trim();}
}

Try below:

public string TxtSurname
{
   get { return _txtSurname;}
   set { _txtSurname=value.Trim();}
}

In the Page load, do not work in the private member, use property instead

protected void Page_Load(object sender, EventArgs e)
{
    TxtSurname = SomeText.Trim();
}

If it's a new Project or it's allowed to modify the code I would create a new Control that derives from TextBox, override the Text property to Trim the base value, then use it instead of the Standard one

I would provider two Properties for the form input.

public class FormInput
{
    public string Surname { get; set; }
    public string ValidatedSurname
    {
        get
        {
            return string.IsNullOrWhiteSpace(Surname)
                   ? string.Empty
                   : Surname.Trim();
        }
    }
}

Why not create the private variable and fill out the properties for your form in full. You could then provide trim() capability on both the get and set for the property and use the property to read and assign your form values instead of using the field directly.

private string _txtSurname;

public string TxtSurname
{
    get
    {
        return string.IsNullOrWhiteSpace(_txtSurname) ? string.Empty : _txtSurname.Trim();
    }
    set
    {
        _txtSurname = value.Trim();
    }
}

That way if you set or get the value without then need to worry about it being trimmed anywhere else

protected void Page_Load(object sender, EventArgs e)
{
    this.TxtSurname = txtSurname.Text();
}

If you moved the form properties into their own class and made the field values private and the properties public (like when you develop where you use a View Model) then you would not be able to assign to the field values directly and would not risk having any untrimmed values. Of course if you're doing this then you only need the set to do the trimming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top