C # trim inside get; set;

I am a complete MVC newbie who has come up with 10 years of web forms. Here is the code I inherited:

namespace sample.Models
{
    public class Pages
    {
        public int PageID { get; set; }
        public string FolderName { get; set; }
    }
}

How can I apply the trim function to the "set" part of this code? Right now this allows spaces at the end of the folder name, and I need to prevent this.

Ok, I included suggestions, however, spaces still remain.
Here is the UI / vs database. The user interface is trimmed correctly, but the full value with spaces is stored in the table:

UI

Database has spaces

+4
source share
7 answers

You need a background field :

public class Pages
{
    public int PageID { get; set; }

    private string _folderName;
    public string FolderName 
    { 
        get { return _folderName; } 
        set { _folderName = value.Trim(); }
    }
}

In the setter method, we use the string method Trim, which

String.

, , .

+14

, . , .

namespace sample.Models
{
    public class Pages
    {
        public int PageID { get; set; }

        private string folderName;
        public string FolderName 
        { 
            get { return folderName; }
            set { folderName = value.Trim(); }
        }
    }
}
+1

:

    public class Pages
    {
         private string _folderName;

         public int PageID { get; set; }

         public string FolderName
         {
              get { return _folderName; }
              set { _folderName = value?.Trim() ?? string.Empty; }
         }
}
+1
public class Pages
{
    public int PageId { get; set; }

    // you need a backing field then you can customize the set and get code
    private string folderName;
    public string FolderName
    {
        get { return this.folderName; }

        // if the fileName can be set to null you'll want to use ?. or you'll get 
        // a null reference exception
        set { this.folderName = value?.Trim(); }
    }
}
+1

Trim , null:

public static class CustomExtensions
{
     public static string TrimIfNotNull(this string value)
     {
         if (value != null)
         {
             value = value.Trim();
         }
         return value;
     }
}

Pages, -

private string _folderName;
public string FolderName
{
    get { return _folderName.TrimIfNotNull(); }
    set { _folderName = value.TrimIfNotNull(); }
}

# 6, , :

public string FolderName 
{ 
    get { return _folderName; } 
    set { _folderName = value?.Trim(); }
}
+1

. . // .

          //About the null issue. You can use this.

          if(String.IsNullOrEmpty(txtusername.Text))
            {
                throw new Exception("Cannot be blank!");
            }

        //You can filter the entry before saving it into the database

        txtpageid.Text = book.PageID.Trim();
        txtfoldername.Text = book.FolderName.Trim();
0
        public string FolderName
        {
            get
            {
                return this.FolderName;
            }
            set
            {
                this.FolderName = value.Trim();
            }
        }
-4
source

Source: https://habr.com/ru/post/1665161/


All Articles