开发者

Reference to a non-shared member requires an object reference vb.net?

How do I fix the following error:

   Error 1: "Reference to a non-shared member requires an object reference."

On Line:

   shortCut = CType(WshShell.CreateShortcut(creationDir & "\" & shortcutName & 
   ".lnk"), IWshRuntimeLibrary.IWshShortcut) 

Here's my full code sample (if needed for context):

Imports IWshRuntimeLibrary
Module MainModule
    Public Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean
        Try
            If Not IO.Directory.Exists(creationDir) Then
                Dim retVal As DialogResult = MsgBox(creationdir & " does not exist. Do you wish to create it?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
                If retVal = DialogResult.Yes Then
                    IO.Directory.CreateDirectory(creationDir)
                Else
                    Return False
                End If
            End If

            Dim shortCut As IWshRuntimeLibrary.IWshShortcut
            shortCut = CType(WshShell.CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)
            shortCut.TargetPath = targetFullpath
            shortCut.WindowStyle = 1
            shortCut.Description = shortcutName
            shortCut.WorkingDirectory = workingDir
            shortCut.IconLocation = iconFile & ", " & iconNumber
            shortCut.Save()
            Return True
      开发者_开发技巧  Catch ex As System.Exception
            Return False
        End Try
    End Function


It means you're using an instance of a class to qualify one of its shared members instead of the class itself. For example:

Class C
    Public Shared x As Integer
End Class
Module M
    Sub S(instance as C)
        dim x1 = instance.X 'warning
        dim x2 = C.X 'proper
    End Sub
End Module


You need to create an instance of WshShell, I think:

Dim NewWshShell As New WshShell

now use newWshShell in place of WshShell

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜