tags:

views:

78

answers:

6

In C#, if we have a class 'Employee' and it will have a property called 'Code' that must consist of 7 characters in the following format: (A-Z) + (1-9)+(0-1)+(0001-9999)

for example 'Code' = A501234 or Z910002

So if we make a property 'Code' in the Class 'Employee' is there any way to force developer when 'Code' is set, to check if it is in the prev format or at least force him to set it to 7 characters, so that, for example, it can cause compiler or build errors?

Thanks in advance for expected cooperation

+1  A: 

You can do this in the setter section of the property.

public string Code
{
    set 
    {
     if (value.Length != 7)
      throw new Exception("Length must be 7.");
     _code = value;
    }
}
astander
+1  A: 

In the setter for the property, you can check the length and throw an error if it is != 7

To check the exact format, you can use regular expressions.

David Stratton
+2  A: 
public string Code
{
    set
    {
        if (value.Length != 7)
        {
            throw new ArgumentOutOfRangeException(...)
        }
        // other validation
        _code = value;
    }
}
Michael Petrotta
+3  A: 
public struct Code
{
    public readonly String Value;

    public Code(String value)
    {
        if (value == null) throw new ArgumentNullException("value");
        if (value.Length != 7) throw new ArgumentException("Must be 7 characters long.");

        // Other validation.

        Value = value;
    }
}
ChaosPandion
A: 

A Regular expression can validate the length and the format all at once.

class Employee
{
   // ...
   private string code;
   public string Code
   {
      set
      {
         Regex regex = new Regex("^[A-Z]{1}[1-9]{1}[0,1]{1}[0-9]{3}[1-9]{1}$");
         Match match = regex.Match(value);
         if (!match.Success)
         {
            throw new ArgumentException("Employee must be in ... format");
         }            
         code = value;
       }
    }
   // ...
}
TJB
+1  A: 

Validating parameter values/formats at runtime could also be accomplished using the Validation Application Block from the MS Enterprise library or something similar.

You would basically specify your validation rules using attributes,

[StringLengthValidator(1, 50, Ruleset = "RuleSetA", 
           MessageTemplate = "Last Name must be between 1 and 50 characters")]
public string LastName
{
    get { return lastName; }
    set { lastName = value; }
}

[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, 
               DateTimeUnit.Year, Ruleset="RuleSetA", 
               MessageTemplate="Must be 18 years or older.")]
public DateTime DateOfBirth
{
    get { return dateOfBirth; }
    set { dateOfBirth = value; }
}

[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", 
             Ruleset = "RuleSetA")]
public string Email
{
    get { return email; }
    set { email = value; }
}

Code snippet from http://msdn.microsoft.com/en-us/library/dd139849.aspx. Don't know of anything that would check at compile time though.

nick