Immediate window behavior differences in C# and VB.NET
Asked Answered
B

2

15

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.

Brasserie answered 16/11, 2011 at 23:47 Comment(0)
N
14

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.

Noman answered 17/11, 2011 at 0:2 Comment(3)
Thanks, that pretty much makes sense. It's still a pain though, especially for simple property evaluation.Brasserie
So basically, just put a ? in front of your statementsRoyer
@Royer absolutely; the question is more or less "why is that necessary?"Noman
A
5

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.

Adnah answered 17/11, 2011 at 0:17 Comment(1)
Thanks for the extra clarification!Brasserie

© 2022 - 2024 — McMap. All rights reserved.