I have noticed that the immediate window in VS 2010 behaves differently when debugging a C# project and a VB.NET project, although I haven't been able to find any specific documentation of this difference.
For C# projects, I can simply type in any expression, and it will be evaluated and displayed, i.e. typing in
foo.bar == "baz"
will output
false
In VB.NET, however, doing the same thing outputs nothing.
I have to put a question mark in front of the expression for it to work.
?foo.bar = "baz"
false
Edit for clarity and my bad example above:
All other expressions exhibit the same behavior, including simple math such as '1 + 2'. Sometimes the error message is different though, as 1 + 2 results in the error 'Labels that are numbers must be followed by colons.'
Is there a way to 'fix' this behavior and make the VB.NET immediate window behave more like the C# one? Having to type a ? in front of every statement can be a pain when using it frequently.
Similar to the Command window, the Immediate window lets you test the code without having to run it. The Intermediate window is used to evaluate, execute statements, or print variable values. To open the Immediate window, navigate to Debug | Windows and select Immediate.
Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.
We can toggle the Immediate Window by clicking the little terminal icon, which is right below the “show watch variables” icon (the glasses). The Immediate Window will only accept input when the application is in a paused state, likely due to hitting a breakpoint.
From the View menu, choose Immediate window (CTRL+G).
The semantics of the immediate windows are just different. In C#, any expression or statement you enter is evaluated, and the result of the evaluation is printed to the window. In VB.NET, you have to enter a complete statement; you can't enter a bare expression. In your example, as you discovered, you need to use the 'Print' statement (the alias for which is ?
) if you want to print anything to the window.
One reason for this is that the semantics of the languages are different. As Bob Kaufman mentioned, =
can be an assignment operator or an equality test. If the VB.NET window worked like the c# window, there would be no way to determine whether a = b
meant "assign b to a" or "evaluate whether b is equal to a".
Assignments do not have a value in VB.NET; a = b = 4
means "evaluate whether b is equal to 4, and assign the result of that evaluation to a." This means that a will either be equal to true or false.
In C#, an assigment is also an expression with a value, so a = b = 4
means "assign the value 4 to b, and assign the value of the expression (b = 4) to a." This means that a will be equal to 4.
The immediate window parser expects a statement if you don't use the ? command. The command
foo.bar = "baz"
is legal in vb.net, it is an assignment statement, giving the bar field or property of the object foo the value "baz". It is however going to complain if bar is a method of the class. Similarly, "1+2" is not a valid statement in vb.net, the ? command helps the interpreter to understand that you meant to evaluate an expression. To turn the = operator from an assignment into a comparison operator, you have to make the parser understand that an expression is being evaluated. ? required. Same thing for "1+2", the vb.net statement parser accepts a number at the start of a statement as a statement label, fit for a GoTo.
The C# language follows the curly brace languages standard where any expression is also a valid statement. So "1+2" is interpreted as a valid statement without help from ? Which is also the reason it needs a separate symbol for the equality operator (==), a parser wouldn't otherwise know the difference between an assignment statement and an expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With