Display a Variable or Method Declaration Line in a Code Window

 < Free Open Study > 



Use the CodeModel Object to Analyze Code

To get a list of all variables and methods in your project, you can call the GetMembersList method shown in the following code. It will loop through all of the CodeElement objects in the project. It calls the GetMembers function, which is also shown in the following code, to append members to the string. You do not have to have any code windows open while this code is executing. The CodeModel is scanned behind the scenes and is actually quite fast.

    Public Function GetMembersList(ByVal PI As Integer, _       Optional ByVal rsCompName       As String = "") _ As String       ' this method and its helper sub GetMethods       ' will list all of the members in the project.       Dim cm As CodeModel       cm = oVB.Solution.Projects.Item(PI).CodeModel       ' Look for all the namespaces and classes in the       ' project.       Dim list As String       Dim ce As CodeElement       On Error Resume Next       For Each ce In cm.CodeElements          If (TypeOf ce Is CodeClass) Then             ' See if that namespace or class contains             ' other classes.             If rsCompName = "" Or _                ce.Name.ToUpper = rsCompName.ToUpper _                Then                GetMembers(ce, list)             End If          End If       Next       Return list    End Function    Sub GetMembers(ByVal ct As CodeElement, _    ByRef list As String)    ' ct could be a namespace or a class.    ' Add it to the list    ' if it is a class.    Static sClass As String    Dim sp As Integer    Dim ep As Integer    On Error Resume Next    If (TypeOf ct Is CodeClass) Then       list &= "Class: " & ct.Name & " Kind: " & _         ct.Kind.ToString & vbCrLf       sClass = ct.Name    ElseIf (TypeOf ct Is CodeFunction) Then       sp = ct.StartPoint.Line       ep = ct.EndPoint.Line       list &= "Class: " & sClass & _          ", Method: " & ct.Name & _          ", Start: " & sp.ToString & _          ", Lines: " & _          (ep - sp + 1).ToString & vbCrLf    ElseIf (TypeOf ct Is CodeVariable) Then       sp = ct.StartPoint.Line       list &= "Class: " & sClass & _       ", Variable: " & ct.Name & _       ", Start: " & sp.ToString & vbCrLf    Else       sp = ct.StartPoint.Line       list &= "Class: " & sClass & _            " Element: " & ct.Name & _            " StartLine: " & sp.ToString & vbCrLf    End If    ' See if there are any nested namespaces or    ' classes that might    ' contain other classes.    Dim ce As CodeElement    For Each ce In ct.Members       If (TypeOf ce Is CodeNamespace) Or _          (TypeOf ce Is CodeClass) Or _          (TypeOf ce Is CodeFunction) Or _          (TypeOf ce Is CodeVariable) Then          GetMembers(ce, list)       End If    Next   End Sub



 < Free Open Study > 



Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net