简体   繁体   中英

Non-generic indexer for C# COM DLL property

I'm trying to create an object array with an index that can be exposed inside Microsoft Access. I've found how to create an indexer class, but since it's generic the property is not exposed inside Access VBA. To be more specific, I'm converting a VB.NET COM DLL that works with Access, and this is a string array property code I'm trying to convert:

    Public Shared _ReportParameters(0 To 9) As String
    Public Property ReportParameters(Optional index As Integer = Nothing) As String
        Get
            Return _ReportParameters(index)
        End Get

        Set(ByVal Value As String)
            _ReportParameters(index) = Value
        End Set
    End Property

Here's the code I've converted to C# that is using an indexer class that cannot be exposed inside the DLL:

    public static string[] _ReportParameters = new string[10];
    public Indexer<string> ReportParameters
    {
        get
        {
            return _ReportParameters.GetIndexer();
        }
    }

Any ideas?

The closest in C# of the VB.NET code you posted is this:

class Thing
{
    public static string[] _ReportParameters = new string[10];
    public string[] ReportParameters { get { return _ReportParameters; } }
}

Which is used as

var thing = new Thing();
var result = thing.ReportParameters[0];
thing.ReportParameters[1] = "Test";

However, an indexer is written as follows:

class Thing
{
    public static string[] _ReportParameters = new string[10];
    public string this[int index]
    {
        get
        {   
            return _ReportParameters[index];
        }
        set
        {
            _ReportParameters[index] = value;
        }
    }
}

Which is used as

var thing = new Thing();
var result = thing[0];
thing[1] = "Test";

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