Search code examples
vb.netoption-explicit

Option Strict On disallows late binding with system.array


I have the following code which return wmi information (unknown array)

For Each objMgmt In oquery.Get()
  For Each theproperty In objMgmt.Properties
    If (TypeOf objMgmt(theproperty.Name) Is System.Array) Then
      myrow(theproperty.Name) = ConvertArray(CType(objMgmt(theproperty.Name), Array)).Trim
    end if                      
  next
next

Function ConvertArray converts this to a string value.

Function ConvertArray(ByVal myarray As System.Array) As String
    Dim tel As Integer
    Dim res As String = ""
    If myarray.Length = 0 Then
        Return ""
    End If
    If myarray.Length = 1 Then
        res = myarray(0).ToString
    Else
        For tel = 0 To myarray.Length - 1
            If TypeOf myarray(tel) Is UInt16 Then
                res = res + "[" + CType(myarray(tel), UInt16).ToString + "] , "
            Else
                res = res + CStr(myarray(tel)) + " , "
            End If
        Next
        res = Mid(res, 1, Len(res) - 2)
    End If
    Return res
End Function

"myarray(tel)" give "Option Strict On disallows late binding" problem when I turn option explicit on. How can I solve this, wmi returns integers or strings depending on the query.


Solution

  • You can do myarray.GetValue(0) instead of myArray(0). This will work even with Option Strict On.