IntegerValidator 类

定义

Int32 值进行验证。

public ref class IntegerValidator : System::Configuration::ConfigurationValidatorBase
public class IntegerValidator : System.Configuration.ConfigurationValidatorBase
type IntegerValidator = class
    inherit ConfigurationValidatorBase
Public Class IntegerValidator
Inherits ConfigurationValidatorBase
继承

示例

下面的代码示例演示如何使用 IntegerValidator 类型。

using System;
using System.Configuration;

namespace Microsoft.Samples.AspNet.Validators
{
    class UsingIntegerValidator
    {
        static void Main(string[] args)
        {
            // Display title.
            Console.WriteLine("ASP.NET Validators");
            Console.WriteLine();

            Console.WriteLine(
                "Set mininum and maximum of 1 and 10 inclusive");

            // Create Validator for the range of 1 to 10 inclusive
            int minIntVal = 1;
            int maxIntVal = 10;
            bool exclusive = false;
            IntegerValidator integerValidator =
                new IntegerValidator(minIntVal, maxIntVal, exclusive);

            int testInt = 0;
            ValidateInteger(integerValidator, testInt);
            testInt = 1;
            ValidateInteger(integerValidator, testInt);
            testInt = 5;
            ValidateInteger(integerValidator, testInt);

            Console.WriteLine();
            Console.WriteLine(
                "Set mininum and maximum of 1 and 10 exclusive");

            // Create Validator for the range of 1 to 10 exclusive
            exclusive = true;
            integerValidator =
                new IntegerValidator(minIntVal, maxIntVal, exclusive);

            testInt = 0;
            ValidateInteger(integerValidator, testInt);
            testInt = 1;
            ValidateInteger(integerValidator, testInt);
            testInt = 5;
            ValidateInteger(integerValidator, testInt);

            Console.WriteLine();
            Console.WriteLine(
                "Determine if an object to validate can be validated.");

            object testObjectToValidate = "a";
            Console.WriteLine("Can validate object of type {0}: {1}",
                testObjectToValidate.GetType(),
                integerValidator.CanValidate(testObjectToValidate.GetType()));
            testObjectToValidate = new int();
            Console.WriteLine("Can validate object of type {0}: {1}",
                testObjectToValidate.GetType(),
                integerValidator.CanValidate(testObjectToValidate.GetType()));

            // Leave output on screen until enter is pressed.
            Console.ReadLine();
        }

        private static void ValidateInteger(IntegerValidator integerValidator, int valuetoValidate)
        {
            Console.Write("Validating integer value of {0}:  ", valuetoValidate);
            try
            {
                integerValidator.Validate(valuetoValidate);
                Console.WriteLine("Validated.");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Failed validation.  Message: {0}", e.Message.ToString());
            }
        }
    }
}
Imports System.Configuration

Namespace Microsoft.Samples.AspNet.Validators
    Module UsingIntegerValidator
        Public Sub Main()

            ' Display title.
            Console.WriteLine("ASP.NET Validators")
            Console.WriteLine()

            Console.WriteLine( _
                "Set mininum and maximum of 1 and 10 inclusive")

            ' Create Validator for the range of 1 to 10 inclusive
            Dim minIntVal As Int32 = 1
            Dim maxIntVal As Int32 = 10
            Dim exclusive As Boolean = False
            Dim validator As IntegerValidator = _
                New IntegerValidator(minIntVal, maxIntVal, exclusive)

            Dim testInt As Integer = 0
            ValidateInteger(validator, testInt)
            testInt = 1
            ValidateInteger(validator, testInt)
            testInt = 5
            ValidateInteger(validator, testInt)

            Console.WriteLine()
            Console.WriteLine( _
                "Set mininum and maximum of 1 and 10 exclusive")

            ' Create Validator for the range of 1 to 10 exclusive
            exclusive = True
            validator = _
                New IntegerValidator(minIntVal, maxIntVal, exclusive)

            testInt = 0
            ValidateInteger(validator, testInt)
            testInt = 1
            ValidateInteger(validator, testInt)
            testInt = 5
            ValidateInteger(validator, testInt)

            Console.WriteLine()
            Console.WriteLine( _
                "Determine if an object to validate can be validated.")

            Dim testObjectToValidate As Object = "a"
            Console.WriteLine("Can validate object of type {0}: {1}", _
                testObjectToValidate.GetType(), _
                validator.CanValidate(testObjectToValidate.GetType()))
            testObjectToValidate = New Integer()
            Console.WriteLine("Can validate object of type {0}: {1}", _
                testObjectToValidate.GetType(), _
                validator.CanValidate(testObjectToValidate.GetType()))

            ' Leave output on screen until enter is pressed.
            Console.ReadLine()
        End Sub

        Sub ValidateInteger(ByVal validator As IntegerValidator, ByVal valueToValidate As Integer)
            Console.Write("Validating integer value of {0}:  ", valueToValidate)
            Try
                validator.Validate(valueToValidate)
                Console.WriteLine("Validated.")
            Catch e As ArgumentException
                Console.WriteLine("Failed validation.  Message: {0}", e.Message.ToString())
            End Try
        End Sub
    End Module
End Namespace

注解

IntegerValidator 用于确保整数满足特定条件。 创建 类的实例时, IntegerValidator 将建立验证条件。 具有两个参数的 IntegerValidator 构造函数可确保所验证的整数同时遵循最小值和最大值。 具有三个参数的 IntegerValidator 构造函数会检查最小值和最大值 Int32 ,以及要验证的值是否在指定范围内。 具有四个参数的 IntegerValidator 构造函数检查前三个参数, Int32 并检查值是否等于特定分辨率。

方法 CanValidate 确定要验证的对象类型是否与预期类型匹配。 正在验证的对象作为 方法的参数 Validate 传递。

构造函数

IntegerValidator(Int32, Int32)

初始化 IntegerValidator 类的新实例。

IntegerValidator(Int32, Int32, Boolean)

初始化 IntegerValidator 类的新实例。

IntegerValidator(Int32, Int32, Boolean, Int32)

初始化 IntegerValidator 类的新实例。

方法

CanValidate(Type)

确定是否可以验证该对象的类型。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)
Validate(Object)

确定对象的值是否有效。

适用于

另请参阅