Dela via


IsTrue Operator

Determines whether an expression is True.

You cannot call IsTrue explicitly in your code, but the Visual Basic compiler can use it to generate code from OrElse clauses. If you define a class or structure and then use a variable of that type in an OrElse clause, you must define IsTrue on that class or structure.

The compiler considers the IsTrue and IsFalse operators as a matched pair. This means that if you define one of them, you must also define the other one.

Compiler Use of IsTrue

When you have defined a class or structure, you can use a variable of that type in a For, If, ElseIf, or While statement, or in a When clause. If you do this, the compiler requires an operator that converts your type into a Boolean value so it can test a condition. It searches for a suitable operator in the following order:

  1. A widening conversion operator from your class or structure to Boolean.

  2. The IsTrue operator on your class or structure.

  3. A narrowing conversion operator from your class or structure to Boolean.

If you have not defined any conversion to Boolean or an IsTrue operator, the compiler signals an error.

Note

The IsTrue operator can be overloaded, which means that a class or structure can redefine its behavior when its operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

Example

The following code example defines the outline of a structure that includes definitions for the IsFalse and IsTrue operators.

Public Structure p
    Dim a As Double 
    Public Shared Operator IsFalse(ByVal w As p) As Boolean 
        Dim b As Boolean 
        ' Insert code to calculate IsFalse of w. 
        Return b
    End Operator 
    Public Shared Operator IsTrue(ByVal w As p) As Boolean 
        Dim b As Boolean 
        ' Insert code to calculate IsTrue of w. 
        Return b
    End Operator 
End Structure

See Also

Tasks

How to: Define an Operator

Reference

IsFalse Operator

OrElse Operator