Visual Guard Class Library Documentation

Using an expression to modify the value of a property

Visual Guard allows you to use an expression based on the Visual Guard expression language to dynamically change the value of a property. The Visual Guard expression language supports logical and arithmetic operators, method invocation and allows to access to the context of the security action.

Visual Guard evaluates the value of the expression at runtime for the current context of the security action and uses the return value to set the property. In this expression, you can use the following variables:

The context of the expression corresponds to the target object on which the action is executed. The type of this context depends on the type of the target of the action. When you define an action for a Windows form, the type of the variable is 'System.Windows.Forms.Form'. When you define an action for a WebForm page, the type is 'System.Web.UI.Page'.

For example, when the action is executed on a Windows Form, the expression 'Text.Length > 0' returns true when the title of the form is not empty.

Evaluation of the expression

At runtime, the condition is evaluated just before executing the action. When the action is declared for a specific event, the value of the condition will be evaluated when the event is raised. Visual Guard resolves the type of the object graph at runtime. if the expression is not valid at runtime because an object is null or a member does not exist, Visual Guard will throw an InvalidActionException exception.

Error on expression at design time

The Visual Guard security action designer tries to check the expression at design time. It parses the expression and evaluates the type of the objects statically declared.

In some cases, Visual Guard cannot evaluate the right type of an object and generates warning. For example, the expression 'Controls['MyCheckBox'].Checked' will generate a warning indicating that 'Checked' is not a property of the 'System.Windows.Forms.Control' class since the 'Controls[]' property returns a 'System.Windows.Forms.Control' object.

If you are sure that the type of the object returned by 'Controls['MyCheckBox']' is a checkbox at runtime, you can ignore this warning.

Examples of expression

The following expression will set to false the 'Enabled' property of  the 'Delete' button when the text of the '_txbCountry' TextBox is not equal to the value of the 'Country' permission argument. When the button is already disabled, the button stay disabled even the country stored in _txtCountry is equal to the country specified in the argument.

#CurrentValue and _txbCountry.Text == #Permission['Country']

The following expression adds a filter on Country to the 'RowFilter' property of a DataView.

#CurrentValue.Length == 0 ? string.Format('Country = ''{0}''',#Permission['Country']) : string.Format('({1}) And Country = ''{0}''',#Permission['Country'], #CurrentValue)

If the current filter is empty and the 'Country' permission argument is equal to 'USA', the expression returns: Country = 'USA'.

If the current filter contains the value 'Price = 0.00 OR Price >= 50.00', the expression returns:  (Price = 0.00 OR Price >= 50.00) And Country = 'USA'

See Also

Visual Guard expression language

How to apply security in your application