简体   繁体   中英

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

Can 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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