Results 1 to 7 of 7

Thread: [2005] Why is this Late Binding?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    [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
    ManagePC - the all-in-one PC management and inventory tool

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Why is this Late Binding?

    This part is the problem:
    vb.net Code:
    1. Select Case Item("ChassisTypes")(0)
    The ManagementBaseObject.Item property is type Object, so that code would expand to this:
    vb.net Code:
    1. Dim myObject As Object = Item("ChassisTypes")
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    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
    Last edited by Ginolard; Jun 21st, 2007 at 04:23 AM.
    ManagePC - the all-in-one PC management and inventory tool

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Why is this Late Binding?

    You just answered your own question. What did I say?
    What type of object are you expecting it to be? That's the type you should be casting as.
    What did you say?
    ChassisType is, in fact, a uInt16 array
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    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.
    ManagePC - the all-in-one PC management and inventory tool

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    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
    ManagePC - the all-in-one PC management and inventory tool

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. For Each Item As ManagementBaseObject In Me.WMIWrapper.RunQuery("select * from Win32_SystemEnclosure")
    2.     Select Case DirectCast(Item("ChassisTypes"), UInt16())(0)
    You simply use the original value casted rather than directly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width