Creating rules programmatically through code

You may need to create Rules through code rather than using the Rule Set Editor.

First, create a RuleSet

C#
RuleSet ruleSet = new RuleSet();

Visual Basic
Dim ruleSet = New RuleSet()

The following listing shows how to Add Rules and sub-RuleSets:

C#
ruleSet.ValidationRules.Add( new ValidationRule("Name") );
ruleSet.ValidationRules.Add( new RuleSet("RuleSetName") );

Visual Basic
ruleSet.ValidationRules.Add( New ValidationRule("Name") )
ruleSet.ValidationRules.Add( New RuleSet("RuleSetName") )

Set the Conditions of a Rule by creating Conditions. Here is an example:

C#
ValidationRule rule = ruleSet.Rules["Name"];
rule.Message = new Literal("You broke a rule.");

ComparisonBase comparison = new GreaterThan();
rule.ValidCondition = comparison;

comparison.RightValue = new Literal(5);
comparison.LeftValue = new DataValue("Order", "OrderID");

Visual Basic
Dim rule As ValidationRule = ruleSet.Rules["Name"]
rule.Message = New Literal("You broke a rule.")

Dim comparison As ComparisonBase = New GreaterThan()
rule.ValidCondition = comparison

comparison.RightValue = New Literal(5)
comparison.LeftValue = New DataValue("Order", "OrderID")

The above code shows you how to create a condition that compares a Property in the data to a literal value.

For more information and details of other types of operations, for example calling methods on your objects with FunctionValues, see the Envalidate library reference, which is available from the help menu of the Rule Set Editor or in the Envalidate folder in the Start Menu.