I'm sure I'm not the first to encounter this. Searching for things like "VB.Net isobject" returns nothing, so the answer is probably too obvious to be worth posting.
My code:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Dim objItem As Object
For Each objItem In colItems
If isObject(objItem) Then
If objItem.StatusCode = 0 Then
IsOnline = True
colItems = Nothing
Exit Function
End If
End If
Next
The error:
'IsObject' is not declared. It may be inaccessible due to its protection level.
I think what I need is
If Not objItem Is Nothing Then
Is that correct?
As mentioned, IsObject()
is decidedly less useful in .NET, but given the context, it looks like you want to test for Nothing
/ null. In which case these will work
If Not objItem Is Nothing Then ...
' or
If objItem IsNot Nothing Then ...
More important is that the code will not compile under Option Strict
:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Object
doesnt not have a ExecQuery
method, so that code requires late binding; the same for objItem.StatusCode
.
The code generally looks like maybe it originated from a script. Using the COM interface and dealing with Object
and late binding is not needed when there is a nice NET wrapper for WMI
. I dont know what you were querying for, this will get the serial number of the BIOS:
Option Strict On
Imports System.Management
Public Class WMI
Friend Shared Function ExecWMIQuery(wmiclass As String, queryItem As String) As String
Dim retVal As String = ""
Dim query = String.Format("SELECT {0} FROM {1}", queryItem, wmiclass)
Using searcher As New ManagementObjectSearcher(query)
For Each item As ManagementObject In searcher.Get
Dim p = item.Properties(queryItem)
If (p IsNot Nothing) AndAlso (p.Value IsNot Nothing) Then
retVal = p.Value.ToString
' should be nothing else...
Exit For
End If
Next
End Using
Return retVal
End Function
End Class
Usage:
Dim mySerial = WMI.ExecWMIQuery("Win32_Bios", "SerialNumber")
Console.WriteLine(mySerial)
I tend to be careful with WMI since you are often at the mercy of whatever the manufacturer chose to include or omit, so there are checks for Nothing
and so forth.
The larger point is that System.Management
exposes typed objects and collections so that you do not have to declare everything As Object
and can use Option Strict
to prevent much worse things from happening.
See also: