Validating properties in C #

suppose I got an interface and inherit a 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 can I check the lines in a co-author class? Is it possible to implement internal properties?

+3
source share
9 answers

Yes, but not using auto-properties. You will need to manually implement the properties using the support 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;
    }
}
+4
source

- ...

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

, , , . , , . , , , , .

+3

, , . (http://msdn.microsoft.com/en-us/library/bb384054.aspx):

# 3.0 , . . , , .

, , . , .

, - .

+2

, . , , , , . DataAnnotations.

+2

. , :

private String _firstName;

public String FirstName
{
     get
     {
          return _firstName;
     }
     set
     {
          //Check value for correctness here:
          _firstName = value;
     }
}
0
private DateTime recruitmentDate;    
public DateTime RecruitmentDate
{
    get { return recruitmentDate; }
    set
    {
        validate(value);
        recruitmentDate = value;
    }
}
0

, / #, .

, " ", .

private int backingStoreVariable;
public property MyProperty
{
    get
    {
        return this.backingStoreVariable;
    }
    set
    {
        this.backingStoreVariable=value;
    }
}

get set.

0

, . , "FirstName" :

public class FirstName
{
    private string _Value;
    public string Value
    {
        get
        {
            return _Value;
        }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException("Value cannot be null");
            if (value.Length > 128)
                throw new ArgumentOutOfRangeException("Value cannot be longer than 128 characters");
            _Value  = value;
        }
    }

    public FirstName(string initialValue)
    {
        Value   = initialValue; //does validation check even in constructor
    }
}

, :

public interface IPersonInfo
{
    FirstName FirstName { get; set; }
    String LastName { get; set; }
}

.. . , , :

public FirstName MyFirstName;
var x = MyFirstName.Value;

, , . - , (ints > 0) count (int >= 0), ..

Strings are more complex because they often have length restrictions in addition to value types (for example, no special characters, numbers, etc. This may be possible to accommodate using the read-only length property specified in the constructor of the inheriting class.

0
source

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


All Articles