I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
I hadn't attended to these simple validators in System.Configuration. Although they seem to be behind the scene part of configuration APIs but can be helpful in some cases.
All these validators are inherited from System.Configuration.ConfigurationValidatorBase. You can find some validators such as DefaultValidator, IntegerValidator, LongValidator, RegexStringValidator, StringValidator, TimeSpanValidator, SubClassTypeValidator and TypeSpanValidator there.
They all have a CanValidate() and a Validate() methods. CanValidate() is a Boolean function and determines if the type passed to it can be validated by that validator. Validate() method gets an object and throws an ArgumentException if that object isn't valid.
IntegerValidator has three overloads and can get a minimum and maximum values for range as well as a Boolean value to specify that if the range is exclusive. Then you can pass an integer value to its Validate() method to see if that value is in the range. If yes, nothing happens otherwise you'll get an ArgumentException.
StringValidator() gets a minimum and maximum length and a string of invalid characters as constructor parameters and checks to see if a string value is valid.
As well as above validator, TimeSpanValidator can get a minimum and maximum TimeSpans and a Boolean value to specify that if range is exclusive and a long value which determines the resolution of range. Its Validate() method checks to make sure if a passed TimeSpan is in range.
RegexStringValidator gets a Regular Expression via constructor and on validation checks if a passed string matches this pattern.
Now I merge above things in a Console Application:
static void Main(string[] args)
{
IntegerValidator intValidator = new IntegerValidator(0, 10);
Console.WriteLine(String.Format
("Type {0} {1} be validated.", typeof(String),
intValidator.CanValidate(typeof(String)) ? "can" : "can't"));
intValidator.Validate(10);
Console.WriteLine("************************\n");
StringValidator stringValidator = new StringValidator(5, 10, "%-");
Console.WriteLine(String.Format
("Type {0} {1} be validated.", typeof(Char),
stringValidator.CanValidate(typeof(Char)) ? "can" : "can't"));
stringValidator.Validate("Keyvan");
Console.WriteLine("************************\n");
TimeSpanValidator timespanValidator = new TimeSpanValidator
(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5));
Console.WriteLine(String.Format
("Type {0} {1} be validated.", typeof(Double),
timespanValidator.CanValidate(typeof(Double)) ? "can" : "can't"));
timespanValidator.Validate(TimeSpan.FromSeconds(4));
Console.WriteLine("************************\n");
RegexStringValidator regexValidator =
new RegexStringValidator(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");
Console.WriteLine(String.Format
("Type {0} {1} be validated.", typeof(String),
regexValidator.CanValidate(typeof(String)) ? "can" : "can't"));
regexValidator.Validate("gholi@javat.com");
Console.WriteLine("************************\n");
Console.Read();
}
You can also write your own validator by inheriting from ConfigurationValidatorBase abstract base class. You need to override Validate() method for your custom validator.
Below is a custom integer validator that checks if an integer value is dividable on a specified integer. I also override CanValidate() method.
public class CustomIntegerValidator :
ConfigurationValidatorBase
{
private int modValue = 0;
public CustomIntegerValidator(int value)
{
this.modValue = value;
}
public override bool CanValidate(Type type)
{
if (type != typeof(Int32))
{
return false;
}
return true;
}
public override void Validate(object value)
{
int intValue = Convert.ToInt32(value);
if (intValue % this.modValue != 0)
{
throw new ArgumentException(String.Format
("Value most be dividable on {0}.",
this.modValue));
}
}
}
A test for my CustomIntegerValidator:
static void Main(string[] args)
{
CustomIntegerValidator customintValidator =
new CustomIntegerValidator(4);
Console.WriteLine(String.Format
("Type {0} {1} be validated.", typeof(Int32),
customintValidator.CanValidate(typeof(Int32)) ? "can" : "can't"));
customintValidator.Validate(8);
Console.Read();
}
Now I pass 7 to Validate() method:
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
MehdiVK
Sep 25, 2006 9:52 AM
#
Leave a Comment