UPDATED WORKAROUND: Intellisense for page level map variables using the Virtual Earth JavaScript Intellisense Helper

UPDATE: AH FOOEY! I feel like a goof:). Turns out my workaround DOESN'T WORK!!! If you look at the logic from the original post, it makes sense that it wouldn't work because “var map = new VEMap();” is before “var map = null;” and therefore you get Intellisense for null. I swear it worked (giving me Intellisense for page level map variable in multiple functions) the first time I tested it. I think I may have been a victim of the JavaScript Intellisense cache not having updated :(. Nonetheless, I was so excited about it working, I jumped the gun with my original post. Yes, I realize the last picture in the original post didn't actually convey what I was trying to accomplish. I showed the wrong scenario in my haste to get a blog post up. Maybe if I would have shown the right scenario, I would have recognized the err of my ways:). This entire post has been rewritten with my "plan B" workaround.

So I have been playing around with other approaches to the workaround described here.  The first thing I tried was:

image_thumb1[1] 

On the surface, this seemed like a reasonable workaround.  However, there were still some scenarios where JavaScript errors would pop up.  For example, switching to 3D mode would cause the map control to raise an error:

image_thumb3[1] 

A similar error would happen when you close the browser.  One could argue this annoyance is a small price to pay for JavaScript Intellisense for the map control:).  However, it is still very annoying.  Good news!  There's a better workaround.  After fiddling with a bunch of ideas, I stumbled upon the following approach:

image_thumb1[2]

Everything JUST WORKS!  You get JavaScript Intellisense at design time and the map is null at run time until it is actually instantiated in pageLoad() .  This makes for a nicer experience since you don't have to put up with the annoying error messages or constantly comment/uncomment code to get Intellisense where you need it.  Of course, in production, you should replace the conditional logic with var map = null for your page level variable.

Interestingly enough, this approach works well in another scenario:

image_thumb3[2]

In this scenario you won't get Intellisense for x above the if statement because Visual Studio does not perform return type analysis.  However, using the same trick, you can see that you get Intellisense after the if statement.  This makes life better in a number of scenarios.  However, it does litter your code a bit.  Use it at your discretion:).  One could write a little utility that scans .js files and removes these entries based on a regular expression.  Maybe we'll look at writing something such a utility for our next release.

None of this is in the current release of the Virtual Earth JavaScript Intellisense Helper, nor is it in the updated code I recently blogged about at Updated Virtual Earth Map Control demo code.  However, I will get both updated in the coming days.  For the sample code, I will update the original blog post when I publish the updated code.  I will make sure we release a new version of the Intellisense helper with these changes soon.

-Marc

Technorati Tags: virtual earth,visual studio,asp.net,ajax