0

The option Strict throws me an error of Late Binding, I always cast the objects to the proper type but in this specific case I don't know how to cast this object.

The warning occurs in the instruction Shell.ToggleDesktop()

Here is the code:

''' <summary>
''' "Shell" CLASSID.
''' MSDN Documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/bb776890%28v=vs.85%29.aspx
''' </summary>
Private Shared ReadOnly CLSIDShell As New Guid("13709620-C279-11CE-A49E-444553540000")

''' <summary>
''' Gets the objects in the Shell.
''' Methods are provided to control the Shell and to execute commands within the Shell.
''' There are also methods to obtain other Shell-related objects.
''' MSDN Documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/bb774094%28v=vs.85%29.aspx
''' </summary>
Private Shared ReadOnly Property Shell As Object
    Get
        If _Shell Is Nothing Then
            _Shell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSIDShell))
            Return _Shell
        Else
            Return _Shell
        End If
    End Get
End Property
Private Shared _Shell As Object = Nothing

''' <summary>
''' Shows or hides the desktop.
''' </summary>
Friend Shared Sub ToggleDesktop()

    Shell.ToggleDesktop()

End Sub
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • What type has that `ToggleDesktop` method as a member? – jmcilhinney Sep 28 '14 at 08:09
  • I don't know, all the info that I have is commented in the code, and this: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537747%28v=vs.85%29.aspx – ElektroStudios Sep 28 '14 at 08:18
  • I preffer to keep the `Activator.CreateInstance` usage to avoid referencing in the project unneeded dlls like the COM Object: `Microsoft Shell And Controls Automation` which is used to perform the same task in the same way (because Is the same), I think the type is called "Shell" or "Shell32" from that COM. – ElektroStudios Sep 28 '14 at 08:28
  • 1
    `Project -> Properties -> Compile` in the list, change the result of Late Binding to "Warning". This allows you to use Object in cases when you are sure it is ok and you know what you are doing – Ňɏssa Pøngjǣrdenlarp Sep 28 '14 at 11:49

1 Answers1

1

You get this error because the type Object do not expose a method named ToggleDesktop.

You can set Option Strict Off for this code file, or create a wrapper and use reflection to invoke the method.

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64