简体   繁体   English

无法访问VB.Net中的属性的索引器

[英]can't access indexer of a property in VB.Net

I have a property Cmd defined as follows 我有一个属性Cmd定义如下

Private _cmd As ADODB.Command

Public ReadOnly Property Cmd() As ADODB.Command
    Get
        Cmd = _cmd
    End Get
End Property

However, when I try to use it like this: 但是,当我尝试像这样使用它时:

y = x.Cmd("abc")

I get: 我得到:

Overload resolution failed because no accessible 'Parameters' accepts this number of arguments. 重载解析失败,因为没有可访问的“参数”接受此数量的参数。

However, this works: 但是,这可行:

y = (x.Cmd)("abc")

Can I change my property definition so that the user does not need the parens? 我可以更改我的属性定义,以便用户不需要parens?

In addition to binarycoder´s solution, I suggest you another one. 除了binarycoder的解决方案之外,我建议您使用另一种解决方案。 If you want to use your code in this way: 如果要以这种方式使用代码:

y = x.Cmd("abc")

You can change your Cmd property to look like this: 您可以将Cmd属性更改为如下所示:

Public ReadOnly Property Cmd(ByVal parameterName As String) As ADODB.Command
        Get
            Return _cmd.Parameters(parameterName)
        End Get
End Property

Hope it helps! 希望能帮助到你!

Can I change my property definition so that the user does not need the parens? 我可以更改我的属性定义,以便用户不需要parens?

No. The crux of the issue is that the default property ( Parameters ) is not an indexed property but instead returns an ADODB.Parameters object. 否。问题的症结在于默认属性( Parameters )不是索引属性,而是返回ADODB.Parameters对象。 Although ADODB.Parameters is has indexed Item property, it is one level too deep. 尽管ADODB.Parameters具有索引的Item属性,但它太深了一层。 Since you can't change ADODB , you might consider adding a helper method. 由于您无法更改ADODB ,因此可以考虑添加一个辅助方法。 You can then use this method instead of your property. 然后,您可以使用此方法而不是您的属性。

Public Function CmdParameter(ByVal parameterName As String) As ADODB.Parameter
    Return Cmd.Parameters(parameterName)
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM