What is ManagementObject's Get method do in this code?

J

Just Me

I got the following code from the doc but find it works the same if disk.Get
is included or not.

The help for ManagementObject.Get says:
Binds to the management object.
Whatever that means.

Can someone describe what Get does in a few more words?
Should it be uncommented in the code below?
Also I haven't found the doc saying to use IsDBNull.
Is that documented someplace?

Thanks a lot for any info

Public Shared Sub DriveProperties(ByRef drive As String)
Dim DiskProperties As PropertyDataCollection
Dim DiskProperty
Dim disk As New ManagementObject("win32_logicaldisk.deviceid=""" & drive &
"""")
'disk.Get()
Console.WriteLine("**** Properties of Drive " & drive)
DiskProperties = disk.Properties
For Each DiskProperty In DiskProperties
If IsDBNull(DiskProperty.Value.ToString()) Then
Console.WriteLine("Name = {0} Value not defined", DiskProperty.Name)
Else
Console.WriteLine("Name= {0} Value={1}", DiskProperty.Name,
DiskProperty.Value.ToString())
End If
Next
disk = Nothing
End Sub
 
L

LarryLard

Just Me said:
I got the following code from the doc but find it works the same if disk.Get
is included or not.

The help for ManagementObject.Get says:
Binds to the management object.
Whatever that means.

Can someone describe what Get does in a few more words?

There is more information in the documentation, specifically in the
topic for the particular overload of Get() that takes no parameters. I
reproduce some of it here for you:
Remarks

The method is implicitly invoked at the first attempt to get or set
information to the WMI object. It can also be explicitly invoked at
the user's discretion, to better control the timing and manner of
retrieval.

Example

Dim o As New ManagementObject("MyClass.Name=""abc""")
string s = o("SomeProperty") 'this causes an implicit Get().

'or :

Dim o As New ManagementObject("MyClass.Name= ""abc""")
o.Get() 'explicitly
'Now it's faster because the object has already been retrieved.
string s = o("SomeProperty");
Should it be uncommented in the code below?

In this case, it won't make any difference.
Also I haven't found the doc saying to use IsDBNull.
Is that documented someplace?

Using IsDBNull when you're not dealing with databases is incorrect. I
think what you want instead of
If IsDBNull(DiskProperty.Value.ToString()) Then

is

If DiskProperty.Value Is Nothing Then
 
J

Just Me

LarryLard said:
There is more information in the documentation, specifically in the
topic for the particular overload of Get() that takes no parameters. I
reproduce some of it here for you:

Remarks

The method is implicitly invoked at the first attempt to get or set
information to the WMI object. It can also be explicitly invoked at
the user's discretion, to better control the timing and manner of
retrieval.

I did see this, but in the example I copied to my post and this one the
statement after the "Get" would implicedly invoke Get so how is it faster?


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top