Thursday, September 9, 2010

Why Does "Go to Definition" And Intellisense Not Work Properly For Referenced Projects in VS.NET

Recently I was working with a solution containing (3) projects in VS.NET2010. When I would right-click an item (i.e. a method) from another project and state "Go To Definition" (F12 key does the same thing), the Object Browser window would open as opposed to going to the actual method or class definition.

My 1st step was to delete the project references between projects, and make sure to re-add them from the "Projects" tab. Upon doing this, the answer of why this problem was occurring appeared:


The project I was working with was a project recently upgraded to VS.NET 2010. Somehow, the other 2 projects in my solution when upgrading to VS.NET 2010 remained targeted at the .NET Framework 3.5. When referenced .dlls target a different Framework, they are treated as file references and not project references and therefore the "Go to Definition" and other Intellisense features related to those references does not work properly.

The fix? Get all projects targeting the same .NET Framework if you can. You can do this by right-clicking the project in "Solution Explorer" and selecting "Properties". Select the "Compile" tab and then select "Advanced Compile Options...". From in this dialog window, you can change the targeted framework of the project. Upon completing this for each of my other (2) projects still targeting the .NET Framework 3.5, I removed the project references and re-added them for good practice to cover all steps possible. I then cleaned and re-built the solution. After I did this the "Go To Definition" functionality and associated Intellisense was working properly again.

Wednesday, September 1, 2010

Why Are My Lambda Functions Throwing An Error at Run Time With a System.MissingMemberException Exception?

Recently I was working in a VS.NET 2010 project and I was creating a multiline Lambda expression function within a method to use. It was pretty straight forward and was syntactically correct. I could build the application successfully, but when hitting that code at compile time I received the following error:

No default member found for type 'VB$AnonymousDelegate_0(Of String,String)'.
System.MissingMemberException occurred
Message=No default member found for type 'VB$AnonymousDelegate_0(Of String,String)'. Source=Microsoft.VisualBasic


The signature of (Of String,String) is not that important and if you are receiving this problem, it would reflect the lambda functions signature of your method. I took the code and placed it in a test harness VS.NET 2010 app, and it both compiled and ran successfully. One thing I immediately noticed is if I hovered over the Function variable declaration it resolved to: Dim MyFormatFunc As <Function(String) As String>. In my problematic application it resolved to nothing.

Well the problem lies in the project setting to allow 'Type Inference'. This was a VS.NET 2005 originating project that has been converted to 2008, then to 2010. If the project originated in 2008 or later, Type Inference is set to 'On' by default. My grandfathered application had it set to 'Off' which meant these anonymous types would be treated as type 'Object' instead of resolving to their appropriate type.

You can change this setting at the page, class, or project level which is what I prefer. To turn on this setting, right-click your project in Solution Explorer within VS.NET (or double click "My Project" to get the same result) which will bring up the project's properties. Click on the 'Compile' tab and change the dropdown for 'Option infer:' from 'Off' to 'On' (pictured below).


Recompile your app and run it again. Your lambda functions will work properly and you will not receive any errors (assuming you coded them correctly to begin with). You will also see the IDE resolves the type at design type if you hover over the declared lambda function variable.