[2005] Why is this Late Binding?
Code:
For Each Item As ManagementBaseObject In Me.WMIWrapper.RunQuery("select * from Win32_SystemEnclosure")
Select Case Item("ChassisTypes")(0)
Case 1
Me.txt_Type.Text = "Other"
Case 2
Me.txt_Type.Text = "Unknown"
Case 3
Me.txt_Type.Text = "Desktop"
Case 4
Me.txt_Type.Text = "Low-profile desktop"
Case 5
Me.txt_Type.Text = "Pizza box"
Case 6
Me.txt_Type.Text = "Mini tower"
Case 7
Me.txt_Type.Text = "Tower"
Case 8
Me.txt_Type.Text = "Portable"
Case 9
Me.txt_Type.Text = "Laptop"
Case 10
Me.txt_Type.Text = "Notebook"
Case 11
Me.txt_Type.Text = "Hand-held"
Case 12
Me.txt_Type.Text = "Docking station"
Case 13
Me.txt_Type.Text = "All-in-one"
Case 14
Me.txt_Type.Text = "Subnotebook"
Case 15
Me.txt_Type.Text = "Space-saving"
Case 16
Me.txt_Type.Text = "Lunch box"
Case 17
Me.txt_Type.Text = "Main system chassis"
Case 18
Me.txt_Type.Text = "Expansion chassis"
Case 19
Me.txt_Type.Text = "Subchassis"
Case 20
Me.txt_Type.Text = "Bus-expansion chassis"
Case 21
Me.txt_Type.Text = "Peripheral chassis"
Case 22
Me.txt_Type.Text = "Storage chassis"
Case 23
Me.txt_Type.Text = "Rack-mounted chassis"
Case 24
Me.txt_Type.Text = "Sealed-case computer"
End Select
Re: [2005] Why is this Late Binding?
This part is the problem:
vb.net Code:
Select Case Item("ChassisTypes")(0)
The ManagementBaseObject.Item property is type Object, so that code would expand to this:
vb.net Code:
Dim myObject As Object = Item("ChassisTypes")
Select Case myObject(0)
That's late binding because the Object class has no default property so you cannot simply index it like that. You have to cast the value of the ManagementBaseObject.Item property to the appropriate type that DOES have a default property. What type of object are you expecting it to be? That's the type you should be casting as.
Re: [2005] Why is this Late Binding?
The problem is that ChassisType is, in fact, a uInt16 array containing a list of all possible ChassisTypes. When you query it, of course, it will only ever contain 1 entry. So, what object should it be?
And your code above is still considered late binding when I turn on Option Strict
Re: [2005] Why is this Late Binding?
You just answered your own question. What did I say?
Quote:
What type of object are you expecting it to be? That's the type you should be casting as.
What did you say?
Quote:
ChassisType is, in fact, a uInt16 array
Re: [2005] Why is this Late Binding?
Heh - I knew that would be your answer as soon as I posted it ;)
I'm just having a little trouble figuring out the code to cast it to an array and then have the select Case statement get the first (and only) element of the aray
This, for example,
Code:
Dim ChassisTypes As Array
For Each Item As ManagementBaseObject In Me.WMIWrapper.RunQuery("select ChassisTypes from Win32_SystemEnclosure")
ChassisTypes = CType(Item("ChassisTypes"), Array)
Select Case Item("ChassisTypes")(0)
Case 1
Me.txt_Type.Text = "Other"
..is obviously wrong.
Re: [2005] Why is this Late Binding?
Ah, figured it out.
Code:
Dim ChassisTypes As Array
For Each Item As ManagementBaseObject In Me.WMIWrapper.RunQuery("select ChassisTypes from Win32_SystemEnclosure")
ChassisTypes = CType(Item("ChassisTypes"), Array)
For Each ChassisItem As UInt16 In ChassisTypes
Select Case ChassisItem
Re: [2005] Why is this Late Binding?
I said that you should be casting as the type the object is. You said that the object is a UInt16 array. It therefore follows that you should be casting as type UInt16(), not type Array. Also, use DirectCast in preference to CType unless you actually need to convert as opposed to cast. You only need to make one change:
vb.net Code:
For Each Item As ManagementBaseObject In Me.WMIWrapper.RunQuery("select * from Win32_SystemEnclosure")
Select Case DirectCast(Item("ChassisTypes"), UInt16())(0)
You simply use the original value casted rather than directly.