IntegerValidator Klasa

Definicja

Zapewnia walidację Int32 wartości.

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

Przykłady

W poniższym przykładzie kodu pokazano, jak używać IntegerValidator typu.

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

Uwagi

Klasa IntegerValidator jest używana do zapewnienia, że liczba całkowita spełnia określone kryteria. Kryteria weryfikacji są ustanawiane podczas tworzenia wystąpienia IntegerValidator klasy. Konstruktor z dwoma IntegerValidator parametrami zapewnia, że zweryfikowana liczba całkowita jest zgodna zarówno z wartością minimalną, jak i maksymalną. Konstruktor z trzema IntegerValidator parametrami sprawdza zarówno wartości minimalne, jak i maksymalne Int32 , a także czy wartość, która ma zostać zweryfikowana, znajduje się w określonym zakresie. Konstruktor z czterema IntegerValidator parametrami sprawdza poprzednie trzy parametry, a także sprawdza, czy Int32 wartość jest równa określonej rozdzielczości.

Metoda CanValidate określa, czy typ obiektu, który jest weryfikowany, jest zgodny z oczekiwanym typem. Weryfikowany obiekt jest przekazywany jako parametr Validate metody .

Konstruktory

IntegerValidator(Int32, Int32)

Inicjuje nowe wystąpienie klasy IntegerValidator.

IntegerValidator(Int32, Int32, Boolean)

Inicjuje nowe wystąpienie klasy IntegerValidator.

IntegerValidator(Int32, Int32, Boolean, Int32)

Inicjuje nowe wystąpienie klasy IntegerValidator.

Metody

CanValidate(Type)

Określa, czy można zweryfikować typ obiektu.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
Validate(Object)

Określa, czy wartość obiektu jest prawidłowa.

Dotyczy

Zobacz też