Range Validator
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |
Class Name: RangeValidator<T>
Attribute Name: RangeValidatorAttribute
Configuration tool name: Range Validator
Description
This validator checks that a value falls in a specified range. The range may be either closed, which means it has both a lower and an upper bound specified, or open, which means that it only has one bound****specified.
The Range Validator can be used with any type that implements the IComparable interface. This includes all numeric types and strings. While it is possible, in code, to use this validator with DateTime types, the Date Time Range Validator may be a better choice because it allows you to take advantage of attributes and configuration.
Properties
The following table lists the Range Validator<T> properties.
Range Validator<T> Properties
Property
Description
LowerBound
This is the low-range boundary value. It can be any IComparable type that is compatible with the target. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes or configuration, the application block attempts to convert the supplied string representation of the value to the correct type at run time.
LowerBoundType
This property determines how to evaluate the LowerBound value. Possible values for LowerBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the LowerBound value. This is the default. The Inclusive value means that the validator allows values that are equal to the LowerBound value. The Exclusive value means that the validator does not allow values that are equal to the LowerBound value.
Negated
This is a Boolean property. If it is set to True, it changes the validator's behavior so that it will fail if the condition is met, rather than when it is not met. The default is False.
Tag
This property is a user-supplied string. Typically, it is used to sort or categorize validation results that are in a log.
UpperBound
This is the upper-range boundary value. It can be any IComparable type that is compatible with the target. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes or configuration, the application block attempts to convert the supplied string representation of the value to the correct type at run time..
UpperBoundType
This property determines how to evaluate the UpperBound value. Possible values for LowerBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the UpperBound value. This is the default. The Inclusive value means that the validator allows values that are equal to the UpperBound value. The Exclusive value means that the validator does not allow values that are equal to the UpperBound value.
MessageTemplate
This property is a user-supplied string. Typically, it describes the validation result and is intended for a log.
MessageTemplateResourceName
If you do not want to use the MessageTemplate property to specify a message, you can use another template by specifying it with the MessageTemplateResourceName value. If you include both a MessageTemplate value and a MessageResourceName value, the MessageTemplate value takes precedence.
MessageTemplateResourceTypeName
If you specify a MessageTemplateResourceName value then you must specify a MessageTemplateResourceTypeName value, which is the type of the template you want to use.
Message Template Tokens
If the message template contains tokens (for instance, "{0}"), the validator will replace these tokens with values when the ValidationResult is created. The tokens supported by the RangeValidator are as follows:
Token |
Meaning |
---|---|
{0} |
The value being validated. |
{1} |
The Key of the value being validated. |
{2} |
The Tag of the validator instance. |
{3} |
The lower bound configured for the validator instance. |
{4} |
The lower bound type (Inclusive, Exclusive, or Ignore) configured for the validator instance. |
{5} |
The upper bound configured for the validator instance. |
{6} |
The upper bound type (Inclusive, Exclusive, or Ignore) configured for the validator instance. |
Examples
The following examples show how to use the validator with attributes and with code.
Attributes
The following code example checks that the Age property is between 0 and 110, inclusive.
public class Person
{
[RangeValidator(0, RangeBoundaryType.Inclusive,
110, RangeBoundaryType.Inclusive)]
int Age
{
get
{
return this.CalculateAge();
}
)
// ...
}
'Usage
Public Class Person
<RangeValidator(0, RangeBoundaryType.Inclusive, 110, _
RangeBoundaryType.Inclusive)> _
ReadOnly Property Age() As Integer
Get
Return Me.CalculateAge()
End Get
End Property
' ...
End Class
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |