Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:'&书信电报;方法1>';和'&书信电报;方法2>';不能让彼此负担过重_C#_.net_Vb.net - Fatal编程技术网

C# 错误:'&书信电报;方法1>';和'&书信电报;方法2>';不能让彼此负担过重

C# 错误:'&书信电报;方法1>';和'&书信电报;方法2>';不能让彼此负担过重,c#,.net,vb.net,C#,.net,Vb.net,我在VB中重写了一个列表 在C#中,代码编译如下: class MyObjectCollection : IList { ... /// <summary> /// Gets or sets the element at the specified index. /// </summary> public MyObject this[int index] { get { return (MyObject)in

我在VB中重写了一个列表

在C#中,代码编译如下:

class MyObjectCollection : IList
{
    ...
    /// <summary>
    /// Gets or sets the element at the specified index.
    /// </summary>
    public MyObject this[int index]
    {
        get { return (MyObject)innerArray[index]; }
        set { innerArray[index] = value; }
    }
    ...
}

编辑:完整的类显示在C版本中有两个索引器。其中一个声明为IList索引器的显式实现:

object IList.this[int index] 
{ 
    get 
    { 
        return ((MyObjectCollection)this)[index]; 
    } 
    set 
    { 
        ((MyObjectCollection)this)[index] = (MyObject)value; 
    } 
} 
这就是为什么您还可以有一个类型化索引器,它只在返回类型上有所不同

你需要在VB.net中做同样的事情,但我的VB.net技能不能胜任这项工作——我看不出如何在VB.net中实现显式接口

或者,您需要使用IList的通用版本来拥有特定类型的索引器。以下是C版本:

类MyObjectCollection:IList
{
私有只读MyObject[]innerArray;
公共MyObject此[int索引]
{ 
获取{return(MyObject)innerArray[index];}
set{innerArray[index]=value;}
} 
} 

我建议继承自,它专门用于创建强类型集合

Public Class MyObjectCollection
   Inherits CollectionBase

签出。

反射器告诉我您可以尝试:


但不能保证

如果我正确理解了这个问题,您目前有一种方法,如:

Public Overrides Property Item(ByVal index As Integer) As MyObject
  Get
    Return DirectCast(innerArray(index), MyObject)
  End Get
  Set(ByVal value As MyObject)
    innerArray(index) = value
  End Set
End Property
您还有一个实现IList接口的方法,如下所示:

Public Property Item(ByVal index As Integer) As Object Implements IList.Item
    Get
        Return DirectCast(innerArray(index), MyObject)
    End Get
    Set(ByVal value As Object)
        innerArray(index) = value
    End Set

End Property
这不起作用,因为方法名称相同,但返回类型不同。您需要做的是更改IList实现方法的名称

尝试以下方法:

Public Property IList_Item(ByVal index As Integer)As Object Implements IList.Item
    Get
        Return DirectCast(innerArray(index), MyObject)
    End Get
    Set(ByVal value As Object)
        innerArray(index) = value
    End Set

End Property

C#代码示例未能为我编译,错误与您从VB.net编译器中得到的错误基本相同。你能发布整个C类吗?我的理解是VB是问题所在,而不是C。编辑问题中显示的完整C类解释了为什么它在C中工作。如果在VB.net中可以实现显式接口,那么这也是解决方案。在VB中,所有接口都是显式实现的。没有隐式实现。不起作用:错误:“具有相同签名的多个定义”。可能更好,甚至私有属性IList_项。。。实现IList.Item
Private Property System.Collections.IList.Item(ByVal index As Integer) As Object
    Get
        Return Me.Item(index)
    End Get
    Set(ByVal value As Object)
        Me.Item(index) = DirectCast(value, MyObject)
    End Set
End Property

Public Default Property Item(ByVal index As Integer) As MyObject
    Get
        Return DirectCast(Me.innerArray.Item(index), MyObject)
    End Get
    Set(ByVal value As MyObject)
        Me.innerArray.Item(index) = value
    End Set
End Property
Public Overrides Property Item(ByVal index As Integer) As MyObject
  Get
    Return DirectCast(innerArray(index), MyObject)
  End Get
  Set(ByVal value As MyObject)
    innerArray(index) = value
  End Set
End Property
Public Property Item(ByVal index As Integer) As Object Implements IList.Item
    Get
        Return DirectCast(innerArray(index), MyObject)
    End Get
    Set(ByVal value As Object)
        innerArray(index) = value
    End Set

End Property
Public Property IList_Item(ByVal index As Integer)As Object Implements IList.Item
    Get
        Return DirectCast(innerArray(index), MyObject)
    End Get
    Set(ByVal value As Object)
        innerArray(index) = value
    End Set

End Property