Validating properties in c#

34,269

Solution 1

Or use DataAnnotations

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx

Solution 2

Yes, but not using auto-properties. You will need to manually implement the properties with a backing field:

private string firstName;

public String FirstName
{
    get
    {
        return firstName;
    }
    set
    {
        // validate the input
        if (string.IsNullOrEmpty(value))
        {
            // throw exception, or do whatever
        }
        firstName = value;
    }
}

Solution 3

Something like this...

private string _firstName;
public string FirstName
{
    get
    {
        return _firstName;
    }
    set
    {
        if (value != "Bob")
          throw new ArgumentException("Only Bobs are allowed here!");
        _firstName = value;
    }
}

Basically, what you're using for properties are the syntactic sugar version. At compile time they create a private member variable and wire up the property to use that variable, as I'm doing manually here. The exact reason for this is so that if you want to add logic you can convert it into a manual one like I have here without breaking the implementation of the interface.

Solution 4

Should also mention validation frameworks if you're getting a bit more sophisticated. They can make validation rules much easier to manage, and will also surface errors to your UI, whilst keeping the rules tied to your models so you don't have to have any repetitive boilerplate validation code. Depending on your framework version, one option is DataAnnotations.

Solution 5

As far as I know, if you use the automatic properties syntax, you lose the ability to access the backing fields. According to the documentation (http://msdn.microsoft.com/en-us/library/bb384054.aspx):

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Attributes are permitted on auto-implemented properties but obviously not on the backing fields since those are not accessible from your source code. If you must use an attribute on the backing field of a property, just create a regular property.

So your only solution is to create regular properties.

Share:
34,269
lexeme
Author by

lexeme

The worst programming community

Updated on July 09, 2022

Comments

  • lexeme
    lexeme almost 2 years

    let's suggest I got an interface and inherit class from it,

    internal interface IPersonInfo
    {
        String FirstName { get; set; }
        String LastName { get; set; }
    }
    internal interface IRecruitmentInfo
    {
        DateTime RecruitmentDate { get; set; }
    }
    
    public abstract class Collaborator : IPersonInfo, IRecruitmentInfo
    {
        public DateTime RecruitmentDate
        {
            get;
            set;
        }
        public String FirstName
        {
            get;
            set;
        }
        public String LastName
        {
            get;
            set;
        }
        public abstract Decimal Salary
        {
            get;
        }
    }
    

    then how do I validate strings in collaborator class? Is it possible to implement inside properties?