对非共享成员的引用需要对象引用

时间:2015-07-28 11:10:27

标签: vb.net visual-studio macros

我在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中开发过,我只需要即时帮助。

1 个答案:

答案 0 :(得分: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