2015-07-28 76 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 
相关问题