Introduction
I have been irritated at times when MS IntelliSense stops working in the middle of programming. Sometimes the whole thing quits and at other times only certain sections of code fail to bring up Auto List Members and Parameter Info. Luckily, today I discovered one reason. Hopefully someone can help determine other situations that cause IntelliSense to fail.
I have grown to depend on IntelliSense when learning new libraries of functions. One such library is the GDI+ library. If you have not yet looked into it, I highly recommend you drop what you are doing and start now. Chances are, you are wasting a lot of time writing code that the GDI+ already does on its own. Anyhow, I was writing a sample application to check out features in the library when my IntelliSense stopped working. Irritated, I decided to figure out why.
After tinkering for a half hour I found one silly line in my code that brought IntelliSense to a stand-still. And you don't even need to be working with GDI+. Any code in VS.NET that uses a comma-list to define an array of object parameters will kill IntelliSense for all code following the definition.
The Problem
The problem is quite specific in this case. An array definition that includes object constructors to create elements confuses VS.NET and shuts down IntelliSense. Such a line is found in the following example.
void OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Point arrayPoints[] = {Point(2,3), Point(5,7), Point(10,5)};
}
The Workaround
Unfortunately, the long-hand version of the same code is the way to go. This is really terrible for cases where more elements could be added or removed later. I won't venture a guess as to how many people would like to chide me for suggesting that code be switched as in this work-around. But it seems that if you want IntelliSense to work, you need to do this (or join me in telling Microsoft about this problem and hope the fix it).
void OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Point arrayPoints[3];
arrayPoints[0] = Point(2,3);
arrayPoints[1] = Point(5,7);
arrayPoints[2] = Point(10,5);
}
A Better Work Around
Special thanks to Anonymous for responding to this article with this work-around. I am adding it to the article so it won't be missed in the messages below. Notice that the array brackets are after the type not the identifier.
void OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Point[] arrayPoints = {Point(2,3), Point(5,7), Point(10,5)};
}
A Sidenote
Fortunately, this problem does not appear to affect simple arrays.
void OnPaint(HDC hdc)
{
Graphics graphics(hdc);
int oddNumbersLessThanTen[] = {1,3,5,7,9};
char crookedLetters[] = "BCDGJOPQRSU";
}