RangeAttribute Class

RangeAttribute Class

Designates the minimum and maximum constraints for the associated member.

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.ValidationAttribute
      System.ComponentModel.DataAnnotations.RangeAttribute

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false)]
public class RangeAttribute : ValidationAttribute

The RangeAttribute type exposes the following members.

  NameDescription
Public methodRangeAttribute(Double, Double)Initializes a new instance of the RangeAttribute class with specified double values as the minimum and maximum limits.
Public methodRangeAttribute(Int32, Int32)Initializes a new instance of the RangeAttribute class with specified integer values as the minimum and maximum limits.
Public methodRangeAttribute(Type, String, String)Initializes a new instance of the RangeAttribute class with the specified minimum and maximum values converted to the specified type.
Top

  NameDescription
Public propertyErrorMessageGets or sets the non-localizable error message to display when validation fails. (Inherited from ValidationAttribute.)
Public propertyErrorMessageResourceNameGets or sets the property name on the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Public propertyErrorMessageResourceTypeGets or sets the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Protected propertyErrorMessageStringGets the localized or non-localized error message. (Inherited from ValidationAttribute.)
Public propertyMaximumGets the maximum allowed value for the range.
Public propertyMinimumGets the minimum allowed value for the range.
Public propertyOperandTypeGets the type of the Minimum and Maximum values.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodFormatErrorMessageApplies formatting to an error message based on the data field where the error occurred. (Overrides ValidationAttribute.FormatErrorMessage(String).)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValidationResultDetermines whether the specified object is valid and returns an object that includes the results of the validation check. (Inherited from ValidationAttribute.)
Protected methodIsValidDetermines whether the specified object is valid. (Inherited from ValidationAttribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodValidateDetermines whether the specified object is valid and throws a ValidationException if the object is not valid. (Inherited from ValidationAttribute.)
Top

You apply the RangeAttribute attribute to a property when you need to specify minimum and maximum values for the property. A validation exception is raised if the value of the property is not within the minimum and maximum values.

If the value of the property is null or an empty string (""), the value will not fail validation for the RangeAttribute attribute. To validate that the value is not null or an empty string, use the RequiredAttribute attribute.

The RangeAttribute class provides the RangeAttribute(Int32, Int32) constructor to specify integer values for the range, and the RangeAttribute(Double, Double) constructor to specify double values for the range. The RangeAttribute class provides the RangeAttribute(Type, String, String) constructor to specify range values that are not integer or double values by enabling you to provide the type for the minimum and maximum values. The type must implement the IComparable interface. For example, to validate a property against a range of decimal values you must provide the type of the Decimal structure and two string values that will be converted to decimal values.

The following example shows how to specify integer range values for one property and decimal range values for another property.


public class Product
{

  [Range(5, 50)]
  public int ReorderLevel { get; set; }

  [Range(typeof(Decimal),"5", "5000")]
  public decimal ListPrice { get; set; }

}


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2016 Microsoft