Visual Guard Expression Language

Estimated reading: 7 minutes 187 views

Note Note
Visual Guard expression language is available only when the version 2.7 (or higher) of Visual
Guard assemblies are referenced in your application.

Operators
Description

Example
== equality 0 == 0
!= inequality 1 != 0
> greater than 1 > 0
>= greater than or equal 1 >= 1
< less than 0 < 1
<= less than or equal 1 <= 1
is checks if an object is compatible with a given type ‘1’ is string
like compares a string against a pattern ‘Abc’ like ‘[A-Z]b*’
between checks if a numeric or string value is between two values 1 between {0,2}

‘efg’ between {‘abc’, ‘xyz’}

matches checks if a string value matches a regular expression ‘5.00’ matches ‘^-?\d+(\.\d{2})?$’
= Assignment (not available in condition) myValue = 1
and logical or conditional AND operator (compatible with integer, enum or boolean
types)
1 == 1 and ‘a’ == ‘a’

MyEnum.Value1 and MyEnum.Value2

1 and 2

or logical or conditional OR operator (compatible with integer, enum or boolean
types)
1 == 1 or ‘a’ == ‘b’

MyEnum.Value1 or MyEnum.Value2

1 or 2

! logical negation operator  (compatible with integer, enum or boolean
types)
!true

!1

!MyEnum.Value1

? : ternary operator. returns the value of the first expression if the condition is
true, otherwise return the value of the second expression.
1 == 1?’abc’:’def’
?? ?? operator returns the left-hand operand if it is not null, or
else it returns the right operand.
MyStringValue??’def’
+ computes the sum of 2 operands (compatible with number, string or datetime
types)
1 + 1

‘abc’ + ‘efg’

date(‘1974-08-24’) + 5

subtracts the second operand from the first (compatible with number, datetime
types)
1-1

date(‘1974-08-24’) – 5

date(‘2004-08-14’) – date(‘1974-08-24’)

* computes the product of its operands. 1 * 2
/ divides its first operand by its second 10 / 5
% computes the remainder after dividing its first operand by its second 5 % 2
^ raises first number to the power of the second one. 2 ^ 5
new used to create objects and invoke constructors new System.Collections.ArrayList()

new int[] {1,2,3}

null represents a null (Nothing in VB.Net) reference
T() Used to obtain the System.Type object for a type T(System.String)

T(System.String, mscorlib)

T(MyNamspace.MyClass[], myassembly)

C() Converts an expression to a specifed type (equivalent to DirectCast in VB.Net or
cast operator in C#)
C(Controls[0], T(System.Windows.Forms.Button))

C( _employees[0],T(Sample.Employee))


Language Reference


Literal expressions

myString == 'Hello World' // returns true if myString is equal to "Hello World"
myString == 'Tony''s Pizza' // returns true if myString is equal to "Tony's Pizza"
avogadrosNumber == 6.0221415E+23 // returns true if avogadrosNumber is equal to 6.0221415E+23
maxValue == 0x7FFFFFFF  // returns true if maxValue is equal to 2147483647
myDate == date('1974/08/24') // returns true if myDate is equal to August 24, 1974
myDate == date('19740824T031030', 'yyyyMMddTHHmmss') // returns true if myDate is equal to August 24, 1974 at 03:10:30 AM
myBoolean == true // return tue if myBoolean value is equal to true
nullValue == null // returns true if nullValue is equal to null

Double.Parse

Decimal.Parse

Single.Parse

DateTime.ParseExact

CultureInfo.InvariantCulture


Relational operators

IComparable

2 == 2 // true
date('1974-08-24') != DateTime.Today  // true
2 < -5.0 // false
DateTime.Today <= date('1974-08-24') // false
'Test' >= 'test' // true

3 in {1, 2, 3, 4, 5}  // true
'Abc' like '[A-Z]b*'  // true
'Abc' like '?'  // false
1 between {1, 5}  // true
'efg' between {'abc', 'xyz'}  // true
'xyz' is int  // false
{1, 2, 3, 4, 5} is IList  // true
'5.0067' matches '^-?\\d+(\\.\\d{2})?

Boolean Logical operators

true and false // returns false
true or false  // returns true
true and !true // returns false

Enumeration Logical operators

namespace Sample
{
    [Flags]
    public enum Access
    {
        NoAccess = 0,
        Read = 1,
        Write = 2,
        Create = 4,
        ReadWrite = Read | Write,
        AllAccess = ReadWrite | Create
    }
}

Sample.AllAccess and Sample.Write // Returns Sample.Write
Sample.Read or Sample.Write // Returns Sample.ReadWrite
Sample.AllAccess and !Sample.Create // Returns Sample.ReadWrite

Integer Logical operators

3 and 1 // returns 1
1 or 2 // returns 3
!1 // returns -2 (equivalent to '~1' in c# or 'Not 1' in VB.Net)


Ternary operators

2 > 1?'abc':'def' // returns 'abc'
'a' != 'b'?'abc':'def' // returns 'def'

1 + 1 // 2
'test' + ' ' + 'string' //'test string'
date('1974-08-24') + 5 // 8/29/1974
1 - 3; // -2
1000.00m - 1e4 // 9000.00
date('2004-08-14') - date('1974-08-24') // returns a TimeSpan equals to 10948.00:00:00
-2 * -3 // 6
2.0 * 3e0 * 4 // 24
6 / -3 // -2
8.0 / 4e0 / 2 // 1
7 % 4 // 3
8.0 % 5e0 % 2 // 1
-2 ^ 4 // 16
1+2-3*8^2/2/2 // -45


Types

1 is int // returns true
DateTime.Today // returns a datetime object containing the current date
new string[] {'abc', 'efg'} // returns an array of strings
System.DateTime == date // returns true
date('2004-08-14').GetType() == T(System.DateTime, mscorlib) // returns true


Constructors

new DateTime(1974, 8, 24) // returns a DateTime object equals to the specified date
new System.Collections.ArrayList(new object[]{'a', 1, date('2004-08-14')}) // returns an ArrayList object containing a string, an int and a datetime.
new T(System.Collections.ArrayList, mscorlib)(10) // returns an ArrayList object


Variables

#this is System.Collections.IList // returns true when the object for which the expression is evaluated implements the IList interface


Properties, Arrays, Lists, Dictionaries, Indexers

namespace Sample
{

          public class Employee
          {
                private int _employeeId;
                private string _firstName;
                private string _lastName;
                private Company _company;
                private MaritalStatus _maritalStatus;

                public Employee(Datetime dateOfBirth, string firstName, string lastName)
                {
                        ....
                }
                public int Id
                {
                    get { return _employeeId; }
                }
                public string FirstName
                {
                    get { return _firstName; }
                    set { _firstName = value; }
                }
                public string LastName
                {
                    get { return _lastName; }
                    set { _lastName = value; }
                }
                public Company Company
                {
                    get { return _company; }
                    set { _company = value; }
                }
                public MaritalStatus Status
                {
                    get { return _maritalStatus; }
                    set { _maritalStatus = value; }
                }
                public static Employee[] GetEmployees()
                {
                    ...
                }
            }
            public enum MaritalStatus
            {
                Married,
                Divorsed,
                Single
            }
            public class Company
            {
                private string _name;
                private ArrayList _employees;
                public Company(string name)
                {
                        ....
                }
                public string Name
                {
                    get { return _name; }
                    set { _name = value; }
                }
                public int AddEmployee(Employee employee)
                {
                     ...
                }
                public int RemoveEmployee(Employee employee)
                {
                    ...
                }
                public Employees[] GetEmployees()
                {
                    ...
                }
                public Employee this[int employeeId]
                {
                    ...
                }
            }
        }


Properties and methods

GetEmployees().Length

#this.GetEmployees().Length

#this.Name.StartsWith('A')

#this._employees

_employees.Clear()


Arrays, Lists, Dictionaries, Indexers

#this[1].Name

[1].Name

#this[1].Status == Sample.MaritalStatus.Divorced

GetEmployees().Length > 1000

_employees[0].Name

Note Note
Since
_employees
is an
ArrayList
object, the values returns by
_employees[0]

is an object. At design time, Visual Guard console generates a warning
indicating that the ‘Name‘ property is not a member of
System.Object‘. In our case, we can ignore this
warning because we are sure that _employees
variable contains only Employee object. If you want
to suppress this warning at design time, you use the C() keyword to cast this value
to Employee type:

C( _employees[0],T(Sample.Employee)).Name

Static (Shared) members 

Sample.Employee.GetEmployees().Length

T(Sample.Employee, sampleassembly).GetEmployees().Length

See Also

Other Resources

// false '5.00' matches '^-?\d+(\.\d{2})?

Boolean Logical operators


Enumeration Logical operators



Integer Logical operators



Ternary operators




Types



Constructors



Variables



Properties, Arrays, Lists, Dictionaries, Indexers



Properties and methods







Arrays, Lists, Dictionaries, Indexers






Note Note
Since
_employees
is an
ArrayList
object, the values returns by
_employees[0]

is an object. At design time, Visual Guard console generates a warning
indicating that the ‘Name‘ property is not a member of
System.Object‘. In our case, we can ignore this
warning because we are sure that _employees
variable contains only Employee object. If you want
to suppress this warning at design time, you use the C() keyword to cast this value
to Employee type:


Static (Shared) members 



See Also

Other Resources

// true

Boolean Logical operators


Enumeration Logical operators



Integer Logical operators



Ternary operators




Types



Constructors



Variables



Properties, Arrays, Lists, Dictionaries, Indexers



Properties and methods







Arrays, Lists, Dictionaries, Indexers






Note Note
Since
_employees
is an
ArrayList
object, the values returns by
_employees[0]

is an object. At design time, Visual Guard console generates a warning
indicating that the ‘Name‘ property is not a member of
System.Object‘. In our case, we can ignore this
warning because we are sure that _employees
variable contains only Employee object. If you want
to suppress this warning at design time, you use the C() keyword to cast this value
to Employee type:


Static (Shared) members 



See Also

Other Resources