Click or drag to resize
CalculationWorks Logo
String Max Length Constraint
Creating a Max Length Constraint

The example shows a type constraint checking max string length. A column with this behavior item will never accept string values longer then MaxLength.

If IsValid() returns false an exceptoin will be thrown.

public class StringMaxLengthConstraint : BcfColumnBehaviorItemBase, IBcfColumnTypeConstraint {

    [BcfMandatory]
    public int MaxLength { get; set; }

    public bool IsValid(object value) {
        var valueToCheck = value as string;
        if (valueToCheck == null) return true;
        return valueToCheck.Length <= MaxLength;
    }
}
See Also