Amr Badawy
Amr Badawy

Reputation: 7673

How to make a string length limited to a specific number?

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

Upvotes: 2

Views: 417

Answers (6)

nick
nick

Reputation: 3458

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.

Upvotes: 1

TJB
TJB

Reputation: 13497

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;
       }
    }
   // ...
}

Upvotes: 0

ChaosPandion
ChaosPandion

Reputation: 78262

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;
    }
}

Upvotes: 3

Michael Petrotta
Michael Petrotta

Reputation: 60902

public string Code
{
    set
    {
        if (value.Length != 7)
        {
            throw new ArgumentOutOfRangeException(...)
        }
        // other validation
        _code = value;
    }
}

Upvotes: 2

David
David

Reputation: 73554

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.

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166346

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;
    }
}

Upvotes: 1

Related Questions