Breaking Visual Studio 2008 Intellisense for Fun and Profit

by Andrew Barber 17. September 2009 17:47

So, I can get pretty aggravated by the most simple things!

I was doing some coding in Visual Studio 2008, and suddenly found that Intellisense was not working on the StreamReader object instance I was coding. I verified that I had declared the object correctly; It was a very small and simple bit of code which read a text file line-by-line. Not only that, the code compiled and worked.

When you frequently code in VS, the Intellisense popup can become like an old friend. It pops up and disappears constantly as you type code. Even when you don't need it to know what members an object has, its constant flicker lets you know you are doing OK.

This is the code I was working with:

List _sources = Cache[STR_Sources_cache_key] as List;
if(_sources == null)
lock(__lockObj)
if(_sources == null) {
string fn = Path.Combine(Path.Combine(RootDir, STR_App_Dir), STR_Sources);
_sources = new List();
using(StreamReader red = new StreamReader(fn)) {
string line = red.ReadLine(); //No Intellisense here!
}
Cache.Insert(STR_Sources_cache_key, _sources, new CacheDependency(fn));
}

Simple enough, right? But within the using block, Intellisense does not work. Outside of that block, it worked fine. One thing might look a bit odd at the top, but is perfectly valid, if odd:

if(_sources == null)
lock(__lockObj)
if(_sources == null) {

I do the tests like that in order to avoid unnecessary locking, and also avoid two threads entering into the code to initialize the _sources object if they happen to arrive at the lock at nearly the same time (which is actually quite possible in this application). Also, the lack of open braces in front of the first if and the lock just takes advantage of the lack of need to wrap those particular blocks in braces. As noted above, that's perfectly valid code, and it compiles and works just fine. However, it actually was sort of the source of the problem. When I changed the code to this:

List _sources = Cache[STR_Sources_cache_key] as List;
if(_sources == null) {  //<--BRACES ADDED
lock(__lockObj) {  //<--BRACES ADDED
if(_sources == null) {
string fn = Path.Combine(Path.Combine(RootDir, STR_App_Dir), STR_Sources);
_sources = new List();
using(StreamReader red = new StreamReader(fn)) {
string line = red.ReadLine(); //Now it works!
}
Cache.Insert(STR_Sources_cache_key, _sources, new CacheDependency(fn));
}
} //<--BRACES ADDED
} //<--BRACES ADDED

It now works!

It actually required a specific sequence of blocks to break Intellisense; A using block, nested inside at least three blocks, two or more of which do not have braces surrounding them.

So, if you use blocks without braces like that, and experience this issue; there's your solution!

Why Eels?

No one can really be certain. But those slimey underwater critters obviously have something going for them!

Links/Profile

Andrew Barber's Profiles:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent the views of employees, contractors or clients of Inkwell Creative Group, LLC in any way.

© Copyright 2008, 2009 Andrew Barber