arrayVariable() As Object vs. arrayVariable As Object() – not the same in Property declaration?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 01:04:36

The problem is that they are Properties so the () is ignored as an Array specifier and thinks of it as an empty parameter collection. Look at how the compiler see them - even the compiler thinks you had an Object and not a Object() and so in your example the A1(0) is an index of an object which is not defined so it thinks you have done some late binding and made it accept an array.

If you are not using a Property and an Object type either declaration is valid.

Dim data() As String

Same as

Dim data As String()

If you highlight either variable the intellisence shows you:

 Dim data() As String

Here's the minimum amount of code required to replicate your issue:

Dim y As Object
Dim x = y(0)

This has nothing to do with the declaration of arrays. You're just trying to convert an Object to an array which option strict doesn't allow.

Additional notes:

(not sure why this has been downvoted)

Changing Object to Integer reveals the first form mentioned in question is not taken as declaration.

Public Class Class1
    Dim _A1 As Integer()
    Dim _A2 As Integer()

    ReadOnly Property A1() As Integer
        Get
            Return _A1 
          ' error at above line: Value of type '1-dimensional array of Integer' cannot
          ' be converted to 'Integer'.
        End Get
    End Property
    ReadOnly Property A2 As Integer()
        Get
            Return _A2
        End Get
    End Property
End Class

Proper way is

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