2010-04-30 2 views
3

VB에서 목록을 덮어 씁니다. 코드를 컴파일하고 다음과 같습니다 C#에서오류 : '<method1>'및 '<method2>'은 서로 오버로드 할 수 없습니다.

는 : VB.NET에서

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; } 
    } 
    ... 
} 

나는 변환 :

Class MyObjectCollection 
    Implements IList   
    ... 

    ''' <summary> ' 
    ''' Gets or sets the element at the specified index. ' 
    ''' </summary> ' 
    Default 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 
    ... 
End Class 

오류 :

'Public Overrides Default Property Item(index As Integer) As MyObject' and 'Public Default Property Item(index As Integer) As Object' cannot overload each other because they differ only by return types

전체 컬렉션 클래스 C#을

public class MyObjectCollection : IList 
{ 
    private ArrayList innerArray; 

    public MyObjectCollection() 
    { 
     innerArray = new ArrayList(); 
    } 

    public int Count 
    { 
     get { return innerArray.Count; } 
    } 

    public bool IsFixedSize 
    { 
     get { return false; } 
    } 

    public bool IsReadOnly 
    { 
     get { return false; } 
    } 

    public bool IsSynchronized 
    { 
     get { return false; } 
    } 

    object ICollection.SyncRoot 
    { 
     get { return null; } 
    } 

    public MyObject this[int index] 
    { 
     get { return (MyObject)innerArray[index]; } 
     set { innerArray[index] = value; } 
    } 

    public int Add(MyObject value) 
    { 
     int index = innerArray.Add(value); 

     return index; 
    } 

    public void AddRange(MyObject[] array) 
    { 
     innerArray.AddRange(array); 
    } 

    public void Clear() 
    { 
     innerArray.Clear(); 
    } 

    public bool Contains(MyObject item) 
    { 
     return innerArray.Contains(item); 
    } 

    public bool Contains(string name) 
    { 
     foreach (MyObject spec in innerArray) 
      if (spec.Name == name) 
       return true; 

     return false; 
    } 

    public void CopyTo(MyObject[] array) 
    { 
     innerArray.CopyTo(array); 
    } 

    public void CopyTo(MyObject[] array, int index) 
    { 
     innerArray.CopyTo(array, index); 
    } 

    public IEnumerator GetEnumerator() 
    { 
     return innerArray.GetEnumerator(); 
    } 

    public int IndexOf(MyObject value) 
    { 
     return innerArray.IndexOf(value); 
    } 

    public int IndexOf(string name) 
    { 
     int i = 0; 

     foreach (MyObject spec in innerArray) 
     { 
      if (spec.Name == name) 
       return i; 

      i++; 
     } 

     return -1; 
    } 

    public void Insert(int index, MyObject value) 
    { 
     innerArray.Insert(index, value); 
    } 

    public void Remove(MyObject obj) 
    { 
     innerArray.Remove(obj); 
    } 

    public void Remove(string name) 
    { 
     int index = IndexOf(name); 
     RemoveAt(index); 
    } 

    public void RemoveAt(int index) 
    { 
     innerArray.RemoveAt(index); 
    } 

    public MyObject[] ToArray() 
    { 
     return (MyObject[])innerArray.ToArray(typeof(MyObject)); 
    } 

    #region Explicit interface implementations for ICollection and IList 
    void ICollection.CopyTo(Array array, int index) 
    { 
     CopyTo((MyObject[])array, index); 
    } 

    int IList.Add(object value) 
    { 
     return Add((MyObject)value); 
    } 

    bool IList.Contains(object obj) 
    { 
     return Contains((MyObject)obj); 
    } 

    object IList.this[int index] 
    { 
     get 
     { 
      return ((MyObjectCollection)this)[index]; 
     } 
     set 
     { 
      ((MyObjectCollection)this)[index] = (MyObject)value; 
     } 
    } 

    int IList.IndexOf(object obj) 
    { 
     return IndexOf((MyObject)obj); 
    } 

    void IList.Insert(int index, object value) 
    { 
     Insert(index, (MyObject)value); 
    } 

    void IList.Remove(object value) 
    { 
     Remove((MyObject)value); 
    } 
    #endregion 
} 
+0

:

같은 것을보십시오. 전체 C# 클래스를 게시 할 수 있습니까? –

답변

0

EDIT : 전체 클래스는 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의 일반 버전을 사용하여 특별히 형식화 된 인덱서가 있어야합니다. 나는 강력한 형식의 컬렉션을 만들기 위해 특별히 존재 CollectionBase, 상속 좋을 것

class MyObjectCollection : IList<MyObject> 
    { 
     private readonly MyObject[] innerArray; 

     public MyObject this[int index] 
     { 
      get { return (MyObject)innerArray[index]; } 
      set { innerArray[index] = value; } 
     } 
    } 
+0

코드 편집에 – serhio

+2

을 추가했습니다. 제 이해는 VB가 문제이며 C# –

+0

마크가 맞지 않습니다. VB에서 문제는 여전히 남아 있습니다. – serhio

1

다음은 C# 버전입니다.

Public Class MyObjectCollection 
    Inherits CollectionBase 

체크 아웃 example.

1

리플렉터는 나에게 말한다 당신 시도 :

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 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 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의 인터페이스를 구현하는 : 내가 제대로 질문을 이해한다면

+0

작동하지 않습니다 : 오류 : "동일한 서명이있는 여러 정의". – serhio

1

, 당신은 현재와 같은 방법이 있습니다 메서드 이름이 같기 때문에 작동하지만 반환 형식이 다릅니다. 당신이해야 할 일은 IList 구현 메서드의 이름을 바꾸는 것이다. 는 C# 코드 예제 당신이 VB.net 컴파일러에서 받고있어 본질적으로 같은 오류와 함께 나를 위해 컴파일에 실패

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 
+0

아마도 더 좋을 수도 있습니다 ** 비공개 ** 속성 IList_Item ... 구현 IList.Item – serhio

관련 문제