arrayVariable()作为对象与arrayVariable作为对象() - 而不是财产申报一样吗? [英] arrayVariable() As Object vs. arrayVariable As Object() – not the same in Property declaration?

查看:254
本文介绍了arrayVariable()作为对象与arrayVariable作为对象() - 而不是财产申报一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到今天,我还以为下面的两个符号相同(编辑:点心是由替换地产

 属性arrayVariable()作为对象
物业arrayVariable作为对象()

今天,我发现,前者会引发错误的 选项严格On不允许后期绑定。,而后者将编译前pression确定 dictionary1.TryGetValue(CSTR(arrayVariable(0)),结果)

请它们之间有什么区别呢?

我总是会使用第二个符号,如果它也被允许指定数组的尺寸。它没有,所以我坚持了第一种形式(不干净的,因为类型规范的一部分 - 括号 - 是前),以横跨声明一致。现在我看到,即使第一个是不通用......

这真的看起来像一个薄弱点的Visual Basic中,对一件事有两种形式及其用法并不简单,但有这样的渔获物。

完整的源$ C ​​$ C重现该问题:

 公共类class1
    昏暗的_A1()作为对象
    昏暗的_A2()作为对象    只读属性A1()作为对象宣言形式一号
        得到
            返回_A1
        到底得的
    高端物业    只读属性A2作为对象()'声明第二形式
        得到
            返回_A2
        到底得的
    高端物业
末级子的Main()
    昏暗的C1作为新的Class1
    昏暗的D1作为新词典(字符串,字符串)
    昏暗的价值作为字符串=
    d1.TryGetValue(CSTR(c1.A1(0))值)'< - 选项严格On不允许后期绑定。
    d1.TryGetValue(CSTR(c1.A2(0))值)'< - 这里没有错误。
结束小组


解决方案

的问题是,他们是属性,在()被忽视的一个数组符和想这是个空的参数集合。看看编译器如何看他们 - 即使编译器认为你有一个对象,而不是一个对象()键,所以在您的示例 A1(0 )是没有定义,因此它认为你已经做了一些后期绑定,并使其接受一个数组对象的索引。

如果你不使用属性和对象类型该声明是有效的。

 昏暗的数据()作为字符串

 昏暗数据作为字符串()

如果您突显变量intellisence表明您:

 昏暗的数据()作为字符串

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

解决方案

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

这篇关于arrayVariable()作为对象与arrayVariable作为对象() - 而不是财产申报一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆