Partilhar via


Expressions in Visual Basic

The managed expression evaluator accepts most expressions written in Visual Basic. In the Immediate window, the expression evaluator only supports single-line statements.

The following sections offer specific information and discuss some of the expression types that are not supported or partially supported:

  • Casts

  • Dynamic Objects

  • Function Evaluation

  • Identifiers and Types

  • Import Aliases

  • Object Variables Containing Intrinsic Types

  • Operators

  • PropertyEvaluation

  • Strings

  • TypeOf Operator

  • Unsupported Keywords

  • Variable Declarations

  • WebMethods

Visual Basic does not support autoexpand rules for displaying the contents of a data type in meaningful form. For more information, see Displaying Elements of a Custom Data Type.

Casts

Simple casts work in the debugger:

A = CStr(B)

Dynamic Objects

The expression evaluator can evaluate variables that are statically typed as dynamic. It can also evaluate objects that implement the IDynamicObject interface. When objects that that implement the IDynamicObject interface are evaluated in the Watch window, a Dynamic View node is added. The Dynamic View node shows object members but does not allow editing the values of the members.

The following features of dynamic objects are not supported:

  • The compound operators +=, -=, %=, /=, and *=

  • Many casts, including numeric casts and type-argument casts

  • Method calls with more than two arguments

  • Property getters with more than two arguments

  • Property setters with arguments

  • Assigning to an indexer

  • The Boolean operators && and ||

Function Evaluation

The debugger supports the evaluation of functions, including overloaded functions. Therefore, you can enter either of the following expressions, and the debugger will call the correct version of the overloaded function:

myFunction (param1)

myFunction (param1, param2)

Evaluating a function in the debugger calls and executes the code for that function. If the function has side effects, such as allocating memory or changing the value of a global variable, evaluating the function in a debugger window will change the state of your program, which can produce unexpected results.

When you set a breakpoint on an overloaded function, the location of the breakpoint depends on how you specify the function. If you specify only the function name, the debugger will set one breakpoint on each overload of that function name. If you specify the complete signature, function name, and full argument list, the debugger sets one breakpoint on the specified overload.

Identifiers and Types

Debugger expressions can use any identifier visible within the current scope. If the debugger is halted in function Magh, for example, you can use most identifiers visible within Magh, including variable names and function names. Local constants are not supported. You can set the value of any variable visible within the current scope.

The debugger can correctly display any variable of a primitive or intrinsic type. For variables of class type, the debugger correctly displays the value based on the derived-most type. If you have an object leo of type Lion, derived from type Cat, you can evaluate leo.Clawlength and get the correct value for an object of type Lion.

Import Aliases

You cannot use import aliases in the debugger windows.

Object Variables Containing Intrinsic Types

Object variables that contain intrinsic variable types, such as integer, are displayed and edited in a manner that may appear counterintuitive. For example, suppose your source code contains object variable like this:

Dim obj As Object = 5

The Watch window shows the value of variable obj as:

5 {Integer}

To change the value of this variable to 6, you would enter:

6

You would not enter:

6 {Integer}

After you edit the value, you will notice that the debugger adds the {Integer} for you.

Operators

The debugger correctly evaluates most operators, including:

  • Arithmetical operators: ( expr1+ expr2, expr1- expr2, expr1* expr2, expr1/ expr2, expr1\expr2, expr1^expr2 , expr1Modexpr2 ).

  • Assignment operators: ( var1= expr2, var1^= expr2, var1*= expr2, var1/= expr2, var1\= expr2, var1+= expr2, var1-= expr2, var1&= expr2).

  • Comparison operators: (expr2< expr2, expr2<= expr2, expr1> expr2, expr1>= expr2, expr1= expr2, expr1<> expr2).

  • Concatenation operators: (expr1& expr2, expr1+ expr2).

  • Logical operators: (expr1And expr2, expr1Or expr2, expr1XOr expr2, expr1AndAlso expr2, expr1OrElse expr2, Notexpr1).

  • Unary operators: ( - expr1, Not expr1, GetType (type) ).

Property Evaluation

The debugger can evaluate properties in any variable window. However, evaluating a property in the debugger can have side effects, such as changing variable values, that affect program results. To protect against side effects caused by accidental evaluation, you can turn property evaluation off in the General, Debugging, Options dialog box.

Strings

In Visual Basic, you can use the Length operator on a string:

mystring.Length

-or-

"hello world".Length

TypeOf Operator

In Visual Basic, you can use the TypeOf operator in the debugger windows:

TypeOf expressionIs type

For example,

TypeOf Me Is Integer

displays the value false.

If you use TypeOf, it must part of an expression that uses Is. If you use TypeOf without Is, you will get the following error message:

Is required

Unsupported Keywords

The following Visual Basic keywords are not supported in debugger window expressions:

  • AddressOf

  • End

  • Error

  • Exit

  • Goto

  • On Error

  • Return

  • Resume

  • Select/Case

  • Stop

  • SyncLock

  • Throw

  • Try/Catch/Finally

  • With

In addition, no namespace or module level keywords, such as End Sub or Module, are supported.

Variable Declarations

You cannot declare explicit new variables in debugger windows.

However, you can assign to an implicit variable in the Immediate window. These implicit variables are scoped to the debugger and not accessible outside the debugger. For example, the statement o = 5 will implicitly create a new variable o and assign the value 5 to it. Such implicit variables are of type Object unless the type can be inferred by the debugger.

WebMethods

You cannot call WebMethods from debugger windows.

See Also

Other Resources

Expressions in the Debugger

Visual Basic Language Reference