2012-11-20 2 views
1

저는 C#을 처음 접했습니다. 어리석은 질문 인 경우 용서해주세요. 오류가 발생했지만 해결 방법을 모르겠습니다. 저는 Visual Studio 2010을 사용하고 있습니다. 이미 커뮤니티 회원들의 수정 사항을 구현했지만 문제는 계속 발생하고 있습니다. 나는이 때문에 같은 상속 클래스에 추상 멤버를 구현하여 해결할 수 coul 읽은 바로는 나에게 오류액세스 수정 자 해결 방법 변경

'GClass1' does not implement inherited abstract member 'System.Collections.ObjectModel.KeyedCollection<string,GClass2>.GetKeyForItem(GClass2)' 

했다

public class GClass1 : KeyedCollection<string, GClass2> 

그것은이 코드 라인 시작

public class GClass1 : KeyedCollection<string, GClass2> 
{ 
    public override TKey GetKeyForItem(TItem item); 
    protected override void InsertItem(int index, TItem item) 
    { 
    TKey keyForItem = this.GetKeyForItem(item); 
    if (keyForItem != null) 
    { 
     this.AddKey(keyForItem, item); 
    } 
    base.InsertItem(index, item); 
} 

그러나이 오류는 '유형 또는 네임 스페이스 이름이 b e TKey/TItem을 (를) 찾을 수 없습니다. ' 그래서 자리 표시 자 유형을 대체했습니다.

현재 코드는 내가 완전히 GetKeyForItem가 보호되는 것을 잊었

public class GClass1 : KeyedCollection<string, GClass2> 
{ 

    public override string GetKeyForItem(GClass2 item); 
    protected override void InsertItem(int index, GClass2 item) 
    { 
    string keyForItem = this.GetKeyForItem(item); 
    if (keyForItem != null) 
    { 
     this.AddKey(keyForItem, item); 
    } 
    base.InsertItem(index, item); 
} 

입니다. 새 오류는 System.Collections.ObjectModel.KeyedCollection.GetKeyForItem (GCL ass2)을 재정의 할 때 액세스 수정자를 변경할 수 없다는 것을 알려줍니다.

가 나는 또한,이 추상적으로 표시되지 않기 때문에 'GClass1.GetKeyForItem (GClass2)는'몸을 선언해야 말을 이상한 오류를 얻고있다 통근

가 액세스 수정 문제에 대한 해결 방법이 있습니까 ', 또는 부분, 누군가가 '표시되지 않았기 때문에 시체를 선언하십시오'라는 오류를 설명 할 수 있습니까?

감사합니다.

+2

I가 좋은 책을 읽고 제안 : 그 방법은 대신 정의했다 단지 protected 접근성을 갖는, 공개적으로 액세스 할 수 있도록하려면, 당신은 그것을 사용하는 새, 별도의 방법을 추가해야합니다 C# 그리고 다시 시작. 이미 경험했듯이 이러한 빠른 수정으로 인해 더 많은 문제가 발생할 수 있습니다. –

답변

2

GetKeyForItem은 기본 추상 클래스에서 보호되어 있으므로 파생 클래스에서 보호되어야합니다.

protected override string GetKeyForItem(GClass2 item) 
{ 
    throw new NotImplementedException(); 

    // to implement, you'd write "return item.SomePropertyOfGClass2;" 
} 
0

:

이 컴파일해야한다 - (방법들이 추상적 않는 한 몸을 가지고 있기 때문에, 그 두 번째 오류의 원인이다 또한, 나는 당신이 그것을 구현하기를 원할 것입니다 가정합니다.) 오류 'GClass1.GetKeyForItem(GClass2)' must declare a body because it is not marked abstract, extern, or partial'은 아마도 을 구현해야하며 단순히 클래스에 선언하지 않아야 함을 의미합니다. 실제로 코드 블럭을 추가해야합니다. 아무 것도하지 않더라도 코드 블록을 추가해야합니다.

protected override string GetKeyForItem(GClass2 item) 
{ 
    // some code 
} 

2

정의 된대로 추상 메소드를 구현해야합니다.

public class GClass1 : KeyedCollection<string, GClass2> 
{ 
    protected override string GetKeyForItem(GClass2 item) 
    { 
     throw new NotImplementedException(); 
    } 

    public string GetKey(GClass2 item) 
    { 
     return GetKeyForItem(item); 
    } 
}