IntegerValidator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 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) |
判斷物件的值是否有效。 |