Option Strict On disallows late binding with system.array

試著忘記壹切 提交于 2019-12-22 10:06:12

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/6749288/option-strict-on-disallows-late-binding-with-system-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!