Validate a property value
This example provides information on how to validate an expression based on properties in Smart
This example provides snippets of XML and C# that could be used within the smart component project. An active station is required for this example.
The topic considers that a SmartComponent project created from Visual Studio exists.
Refer to topic Creating a Smart
Note
The code in this topic can be tested using the default RobotStudio Smart Component template.
Open the
SmartComponent1.xml
file. Add the following property. Setting the CustomValidation property to true allows the property to be validated. The value of the expression can be provided within the XML file. The type of object DynamicProperty supports is defined by the valueType property.<Properties> <DynamicProperty name="Expression" valueType="System.String" value="a+b"> <Attribute key="CustomValidation" value="true"/> <Attribute key="AddToDisplayName" value="true"/> </DynamicProperty> </Properties>
Open the
SmartComponent1.en.xml
file. Provide descriptions for the component and the property.<SmartComponent name="SmartComponent1" description="Evaluates a mathematical expression"> <DynamicProperty name="Expression" description="Expression to evaluate. Supports +, -, *, /, % (remainder), ^ (power), sin, cos, tan. Numeric properties are automatically added for other identifiers."/> </SmartComponent>
Open the "CodeBehind.cs" file and add the overriding method QueryPropertyValueValid. When the expression changes, Query
Property is triggered. The expression is checked for validity and the result is returned. Following are the steps to be followed to achieve the above mentioned task:Value Valid(Smart Component, Dynamic Property, object) - Check whether the expression is null or empty.
if (string.IsNullOrEmpty((string)newValue)) return ValueValidationInfo.Valid;
- Create an object of Math
Expression for the expression to be validated.MathExpression expr = new MathExpression((string)newValue);
- Return the appropriate value code.
if (!expr.IsValid) return new ValueValidationInfo(ValueValidationResult.InvalidSyntax);
The entire example:
public override ValueValidationInfo QueryPropertyValueValid(SmartComponent component, DynamicProperty property, object newValue) { if (property.Name == "Expression") { if (string.IsNullOrEmpty((string)newValue)) return ValueValidationInfo.Valid; MathExpression expr = new MathExpression((string)newValue); if (!expr.IsValid) return new ValueValidationInfo(ValueValidationResult.InvalidSyntax); } return ValueValidationInfo.Valid; }
- Check whether the expression is null or empty.
Compiling the Code
Note
In order for the Library Compiler to find the code behind assembly asset it needs to be located in the same directory as the Library XML file. The template project contains the following Post-Build Event, that copies the assembly to the right place.
copy /y "$(TargetPath)" "$(ProjectDir)"
The LibraryCompiler.exe
is executed as a Post-Build Event, which means it is executed after the C# project has been built, and the code behind asset has been created.
"%ProgramFiles(X86)%\ABB\RobotStudio {version}\bin\LibraryCompiler.exe" "$(ProjectDir)\SmartComponent1.xml" "$(ProjectDir)\"
Required Namespaces
ABB.