Mateusz Szymański
Mateusz Szymański

Reputation: 117

Reference to non-shared member requires an object reference

I am writing macro for Visual Studio in VB:

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

And I got this after compilation:

Error (17,0): error BC30469: Reference to a non-shared member requires an object reference.

Have you any ideas what is wrong? I didn't develop in VB earlier and I just need instant help.

Upvotes: 0

Views: 2181

Answers (1)

James Thorpe
James Thorpe

Reputation: 32202

DTE is a type, as well as what you've given to the name of a parameter in your Run method. When used in this line:

proj = DTE.ActiveSolutionProjects(0)

It's using the type, rather than an instance of an object. You need to pass through the variable into your method:

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

Upvotes: 2

Related Questions