BUG: EnvDTE.CodeElement.StartPoint.Line wrong result with duplicated 'using' statement.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: September 2012   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

This article explains a bug in the EnvDTE.CodeElement.StartPoint.Line property with duplicated "using" statements.

More information

The following addin reproduces the problem. Load it when the active document is a C# code file with the following two lines:

using System;
using System;

The start line returns 1 for both code elements.

Notice that duplicated "using" statements produce a warning but is is legal and the code is built correctly.

Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
      ByRef custom As Array) Implements IDTExtensibility2.OnConnection

      _applicationObject = CType(application, DTE2)
      _addInInstance = CType(addInInst, AddIn)


         Select Case connectMode

            Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup

               For Each ce As CodeElement In _applicationObject.ActiveDocument.ProjectItem.FileCodeModel.CodeElements

                  MessageBox.Show("Line: " & ce.StartPoint.Line.ToString())


         End Select

      Catch ex As Exception


      End Try

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) _
      Implements IDTExtensibility2.OnDisconnection
   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

End Class

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this