Share via


Property Comparison 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: PropertyComparisonValidator

Attribute Name: PropertyComparisonAttribute

Configuration tool name: Property Comparison Validator

Description

This validator compares the value to be checked with the value of a property.

Properties

The following table lists the Property Comparison Validator properties.

Property Comparison Validator Properties

Property

Description

ComparisonOperator

This property determines how the two properties are compared. The possible comparisons are Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, and LessThanEqual.

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.

PropertyToCompare

This property is a the name of the property to compare

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results that are in a log.

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 MessageTemplateResourceName 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 PropertyComparisonValidator are as follows:

Token

Meaning

{3}

The value against which the current value is being compared.

{4}

The name of the property against which the current value is being compared.

{5}

The operator being used for comparison (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual).

Examples

The following examples show how to use the validator with attributes and with code.

Attributes

The following code example checks that, for an AuctionItem object, the current bid is greater than or equal to the minimum bid.

public class AuctionItem
{
   public double MinimimumBid
   {
      get
      {
         return minimumBid;
      }
   }
   [PropertyComparisonValidator("MinimumBid", 
                                 ComparisonOperator.GreaterThanEqual)]
   public double CurrentBid
   {
      get
      {
         return currentBid;
      }
   }

    // ...
}
'Usage
Public Class AuctionItem
    ReadOnly Property MinimumBid(ByVal _minimumBid As Double)
        Get
            Return _minimumBid
        End Get
    End Property
    <PropertyComparisonValidator("MinimumBid", ComparisonOperator.GreaterThanEqual)> _
    ReadOnly Property CurrentBid(ByVal _currentBid As Double)
        Get
            Return _currentBid
        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.