Until today, I thought that the following two notations are the same (edit: Dim
was replaced by Property
)
Property arrayVariable() As Object
Property arrayVariable As Object()
Today I found that former one throws error Option Strict On disallows late binding.
while the latter compiles OK in expression dictionary1.TryGetValue(CStr(arrayVariable(0)), result)
.
Please what is the difference between them?
I would always use the second notation if it also allowed to specify the array dimensions. It doesn't, so I stuck with the first form (less clean one, because part of type specification - parenthesis - are before As
) in order to be consistent across declarations. And now I see that even the first one isn't universal...
It really looks like a weak point of Visual Basic that two forms exist for one thing and their usage is not straightforward but has catches like this.
Full source code reproducing the issue:
Public Class Class1
Dim _A1() As Object
Dim _A2() As Object
ReadOnly Property A1() As Object ' 1st form of declaration
Get
Return _A1
End Get
End Property
ReadOnly Property A2 As Object() ' 2nd form of declaration
Get
Return _A2
End Get
End Property
End Class
Sub Main()
Dim c1 As New Class1
Dim d1 As New Dictionary(Of String, String)
Dim value As String = ""
d1.TryGetValue(CStr(c1.A1(0)), value) '<- Option Strict On disallows late binding.
d1.TryGetValue(CStr(c1.A2(0)), value) '<- No error here.
End Sub