本文整理汇总了C#中System.Configuration.IntegerValidator类的典型用法代码示例。如果您正苦于以下问题:C# IntegerValidator类的具体用法?C# IntegerValidator怎么用?C# IntegerValidator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IntegerValidator类属于System.Configuration命名空间,在下文中一共展示了IntegerValidator类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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());
}
}
}
}