I am primarily a C# developer, I will admit up front that I dislike VB.Net and will avoid using it when I can. I am not going to get into why here because I don’t want to spark all kind of needless arguments. Let’s just say I am heavily biased towards C# just like I am heavily biased towards using and loving Microsoft Products. I am a Microsoft whore – it keeps me employed so I feel indebted to it…and I like it…makes me feel dirty…

So I made a grimey discovery yesterday and I want to share it here because this is a big gotcha. When I use the immediate window with C# I just type in whatever it is that I want to inspect and hit return. I started using the immediate window with VB.Net recently and I kept getting this meaningless error that read:

“Property access must assign to the property or use its value”

This is a screen capture of my immediate window. The first line is the failure and the second line is the right way to do it with VB.Net.

This was very troubling to me at first because this is not the first time I have ever used the immediate window, so I then remembered that I had to do something with a question mark, but I thought that was only for the Command Window, apparently I was wrong… kind of.

I did a google search and I found this page:  http://forums.asp.net/t/1091662.aspx/1

I had a head palm moment and realized that you are really supposed to use a “?” before your arguments in the immediate window. I never had to bother with this before for C#, it appears to only happen with VB.Net.

So the Gotcha of today is…

  • When using an immediate window with VB.Net you are required to start your arguments with a question mark “?”.
  • When using an immediate window with C# you are not required in most instances to use a question mark before your arguments. I can’t prove this, but if you look at this link http://msdn.microsoft.com/en-us/library/f177hahy(v=vs.90).aspx you will see that they show the example with VB.Net and don’t bother with C#, I think that means something.
  • When you want to evaluate a statement (IE: Set a value) in the immediate window with VB.Net, do not include the question mark “?” because you will just receive an unhelpful error. This is not required in C#.

3 thoughts on “Visual Studio’s Immediate Window – C# vs VB.Net

  1. Thank you very much, I appreciate that. You are my first commentator by the way so congrats on that. My goal for this blog is to archive all of the stupid stuff that I encounter on a daily basis and just share it with everyone. There is nothing more frustrating than not having an answer to an obscure "almost never happens" problem.

  2. To use without "?" use Watch window. In Command/Immediate window always use "?". If your code in immediate window exceeds a line, you can write whole thing in one line with ":" as line seperator. This logic is consistent in VB/VB.NET always. Unlike C#, VB always needs "?" in command window irrespective of use cases.

Leave a Reply

Your email address will not be published. Required fields are marked *