|
|
Validation
|
The ValidationService type exposes the following members.
| Name | Description | |
|---|---|---|
| ValidationService | Initializes a new instance of the ValidationService class. |
| Name | Description | |
|---|---|---|
| AddValidation | Adds a new validation. | |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) | |
| Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) | |
| GetHashCode | Serves as the default hash function. (Inherited from Object) | |
| GetType | Gets the Type of the current instance. (Inherited from Object) | |
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) | |
| ToString | Returns a string that represents the current object. (Inherited from Object) | |
| Validate | Executes all validations. |
| Name | Description | |
|---|---|---|
| GetEnumValueOrDefault |
Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) | |
| GetEnumValueOrDefaultT |
Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) |
using System;
using System.Collections.Generic;
using GSF.Validation;
class Program
{
static void Main(string[] args)
{
// Dictionary where captured user input is saved.
Dictionary<string, string> input = new Dictionary<string, string>();
// Validation service that will validate user input.
ValidationService validation = new ValidationService(delegate(string source)
{
string value;
if (input.TryGetValue(source, out value))
return input[source];
else
return string.Empty;
});
// Add validation rules to the validation service.
validation.AddValidation("Name", new NonNullStringValidator());
validation.AddValidation("Email", new EmailAddressValidator());
// Capture user input.
Console.Write("Enter name: ");
input["Name"] = Console.ReadLine();
Console.Write("Enter email: ");
input["Email"] = Console.ReadLine();
Console.WriteLine("");
// Validate user input.
string errors;
if (!validation.Validate(out errors))
Console.WriteLine(errors);
else
Console.WriteLine("No validation errors were found!");
Console.ReadLine();
}
}