引用非共享成员需要一个对象引用vb.net?

时间:2009-12-29 01:10:09

标签: vb.net shortcut

如何修复以下错误:

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

在线:

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

这是我的完整代码示例(如果需要上下文):

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

2 个答案:

答案 0 :(得分:2)

这意味着您正在使用类的实例来限定其共享成员之一而不是类本身。例如:

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

答案 1 :(得分:0)

我认为你需要创建一个WshShell实例:

Dim NewWshShell As New WshShell

现在使用newWshShell代替WshShell