开发者

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

I have a property Cmd defined as follows

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")

Ca开发者_StackOverflow中文版n I change my property definition so that the user does not need the parens?


In addition to binarycoder´s solution, I suggest you another one. If you want to use your code in this way:

y = x.Cmd("abc")

You can change your Cmd property to look like this:

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?

No. The crux of the issue is that the default property (Parameters) is not an indexed property but instead returns an ADODB.Parameters object. Although ADODB.Parameters is has indexed Item property, it is one level too deep. Since you can't change ADODB, you might consider adding a helper method. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜