IntegerValidator IntegerValidator IntegerValidator IntegerValidator Class

定義

Int32 値の検証について説明します。Provides validation of an Int32 value.

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

IntegerValidator型を使用する方法を次のコード例に示します。The following code example demonstrates how to use the IntegerValidator type.

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クラスは、整数が特定の条件を満たしていることを確認するために使用されます。The IntegerValidator class is used to ensure that an integer meets specific criteria. 検証の条件は、 IntegerValidatorクラスのインスタンスが作成されるときに設定されます。The criteria for validation are established when an instance of the IntegerValidator class is created. 2 IntegerValidatorつのパラメーターを持つコンストラクターは、検証対象の整数が最小値と最大値の両方に準拠していることを保証します。The IntegerValidator constructor with two parameters ensures that the integer being verified adheres to both a minimum and a maximum value. 3 IntegerValidatorつのパラメーターを持つコンストラクターは、最小値Int32と最大値の両方をチェックし、検証対象の値が指定された範囲内にあるかどうかも確認します。The IntegerValidator constructor with three parameters checks both the minimum and maximum Int32 values, as well as whether the value to be validated is within the specified range. 4 IntegerValidatorつのパラメーターを持つコンストラクターは、前の3つのパラメーター Int32をチェックし、値が特定の解像度と等しいかどうかもチェックします。The IntegerValidator constructor with four parameters checks the previous three parameters and also checks whether the Int32 value is equal to a specific resolution.

メソッドCanValidateは、検証対象のオブジェクトの型が、予期される型と一致するかどうかを判断します。The CanValidate method determines whether the object type being validated matches the expected type. 検証対象のオブジェクトは、 Validateメソッドのパラメーターとして渡されます。The object being validated is passed as a parameter of the Validate method.

コンストラクター

IntegerValidator(Int32, Int32) IntegerValidator(Int32, Int32) IntegerValidator(Int32, Int32) IntegerValidator(Int32, Int32)

IntegerValidator クラスの新しいインスタンスを初期化します。Initializes a new instance of the IntegerValidator class.

IntegerValidator(Int32, Int32, Boolean) IntegerValidator(Int32, Int32, Boolean) IntegerValidator(Int32, Int32, Boolean) IntegerValidator(Int32, Int32, Boolean)

IntegerValidator クラスの新しいインスタンスを初期化します。Initializes a new instance of the IntegerValidator class.

IntegerValidator(Int32, Int32, Boolean, Int32) IntegerValidator(Int32, Int32, Boolean, Int32) IntegerValidator(Int32, Int32, Boolean, Int32) IntegerValidator(Int32, Int32, Boolean, Int32)

IntegerValidator クラスの新しいインスタンスを初期化します。Initializes a new instance of the IntegerValidator class.

メソッド

CanValidate(Type) CanValidate(Type) CanValidate(Type) CanValidate(Type)

オブジェクトの型を検証できるかどうかを判断します。Determines whether the type of the object can be validated.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)
Validate(Object) Validate(Object) Validate(Object) Validate(Object)

オブジェクトの値が有効かどうかを判断します。Determines whether the value of an object is valid.

適用対象

こちらもご覧ください