2015-07-28 74 views
0

我在VB寫的宏爲Visual Studio:參考非共享成員需要的對象引用

Imports EnvDTE 
Imports EnvDTE80 
Imports Microsoft.VisualBasic 

Public Class C 
    Implements VisualCommanderExt.ICommand 

    Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run 
     FileNamesExample() 
    End Sub 

    Sub FileNamesExample() 
     Dim proj As Project 
     Dim projitems As ProjectItems 

     ' Reference the current solution and its projects and project items. 
     proj = DTE.ActiveSolutionProjects(0) 
     projitems = proj.ProjectItems 

     ' List the file name associated with the first project item. 
     MsgBox(projitems.Item(1).FileNames(1)) 
    End Sub 

End Class 

我編譯之後得到這個:

錯誤(17,0):錯誤BC30469:對非共享成員的引用需要對象引用。

你有什麼想法是什麼錯?我之前沒有在VB中開發,我只需要即時幫助。

回答

2

DTE是一種類型,以及您在Run方法中給出的參數名稱。當用於此行時:

proj = DTE.ActiveSolutionProjects(0) 

它使用的是類型而不是對象的實例。您需要將變量傳遞到您的方法中:

Imports EnvDTE 
Imports EnvDTE80 
Imports Microsoft.VisualBasic 

Public Class C 
    Implements VisualCommanderExt.ICommand 

    Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run 
     FileNamesExample(DTE) 
    End Sub 

    Sub FileNamesExample(DTE As EnvDTE80.DTE2) 
     Dim proj As Project 
     Dim projitems As ProjectItems 

     ' Reference the current solution and its projects and project items. 
     proj = DTE.ActiveSolutionProjects(0) 
     projitems = proj.ProjectItems 

     ' List the file name associated with the first project item. 
     MsgBox(projitems.Item(1).FileNames(1)) 
    End Sub 

End Class 
相關問題