Compartilhar via


Expressions in Visual Basic 

This topic applies to:

Visual Studio Edition

Visual Basic

C#

C++

J#

Express

Yes

No

No

No

Standard

Yes

No

No

No

Pro/Team

Yes

No

No

No

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:

  • Identifiers and Types

  • Function Evaluation

  • Operators

  • Unsupported Keywords

  • Strings

  • Casts

  • TypeOf Operator

  • Object Variables Containing Intrinsic Types

  • Property Evaluation

  • WebMethods

  • Variable Declarations

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)

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 , expr1 Mod expr2 ).

  • 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: (expr1Andexpr2, expr1Orexpr2, expr1XOrexpr2, expr1AndAlsoexpr2, expr1OrElseexpr2, Not expr1).

  • 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 expressionIstype

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 Reference