Enum Conversion 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: EnumConversionValidator

Attribute Name: EnumConversionValidatorAttribute

Configuration tool name: Enum Conversion Validator

Description

This validator checks that a string can be converted to a value in a specified enum type. For example, it can check that "Blue" can be converted to a value in the Color enumerator.

Properties

The following table lists the Enum Conversion Validator properties.

Enum Conversion Validator Properties

Property

Definition

EnumType

This property is the type of the enum. You can either enter the type or you can select it with the Type Selector – System.Enum dialog box.

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.

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

Token

Meaning

{3}

The type of enum to which the validator will attempt to convert.

Examples

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

Attributes

The following code example checks that the sales unit is a string that can be converted to a value of the Unit enumeration. For example, the string "Each" is allowed.

public class Product
{
   enum Unit   {
      Each, Dozen, Gross
   }
   [EnumConversionValidator(typeof(Unit))]
   public string SalesUnit
   {
      get
      {
         return salesUnit;
      }
}
// ...
}
'Usage
Public Class Product
    Enum Unit
        _Each
        _Dozen
        _Gross
    End Enum
    <EnumConversionValidator(GetType(Unit))> _
    ReadOnly Property SalesUnit(ByVal _salesUnit As String)
        Get
            Return _salesUnit
        End Get
    End Property
End Class

API

The following code example constructs an EnumConversionValidator instance that can be used to check whether the sales unit is a string that is convertible to a value of the Unit enumeration. For example, the string "Each" would be allowed.

public class Product
{
   enum Unit
   {
      Each, Dozen, Gross
   }
   readonly Validator UnitValidator = new EnumConversionValidator(typeof(Unit));
// ...
}
'Usage
Public Class Product
    Enum Unit
        _Each
        _Dozen
        _Gross
    End Enum
    ReadOnly UnitValidator As Validator = New _
EnumConversionValidator(GetType(Unit))
    ' ...
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.