2012-11-23 43 views
1

所以我想爲我的一個項目使用幾個ArrayLists,並且我去了尋找合成器的msdn網頁。然而,在應用它之後,錯誤列表會拋出18個錯誤,其中16個是關於「WindowsFormsApplication7.clsAL」沒有實現接口成員「System.ICloneable.Clone()」的,其中2個是關於「類型或名稱空間無法找到名稱'ComVisibleAttribute'或'ComVisibleAttributeAttribute'(您是否缺少使用指令或程序集引用?)「。這是我的代碼:Arraylist問題

using System; 
using System.Collections; 
using System.Collections.Generic; 

namespace WindowsFormsApplication7 
{ 
    [SerializableAttribute] 

    class clsAL : IList, ICollection, IEnumerable, ICloneable 
    { 


    public ArrayList dir = new ArrayList(); 
     public ArrayList time = new ArrayList(); 
    } 



    } 
//  

我錯過了什麼嗎?

+1

什麼是你正在嘗試待辦事項的最終結果,所有的接口都不再需要只是使用ArrayList的 –

+4

拜託,不要使用'ArrayList',使用一般的'名單'代替。 – svick

回答

0

是的,我會說你錯過了一些東西!

您正在實現一堆接口,但實際上並沒有重寫(甚至指定)那些接口所需的任何東西。那種擊敗實現接口的全部目的...

3

你可能看過ArrayList Class

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class ArrayList : IList, ICollection, 
     IEnumerable, ICloneable 

所以,很顯然你沒有在你的代碼兩件事情:

  1. 使用的變量dirtime類型ArrayList(你只需要這個)
  2. 試圖重新實現ArrayList(絕對不同的任務,你不需要n使用ArrayList),將所有這些接口添加到類聲明中。

要使用ArrayList你不需要你的類來實現(使用)Arrayist的接口(和/或使用屬性)。所以,只是從你的類聲明中刪除:

//all attributes removed 
class clsAL //all interfaces removed 
{ 
    public ArrayList dir = new ArrayList(); 
    public ArrayList time = new ArrayList(); 

} 

如果你的類必須實現某種接口,它應該包含的實際執行(明確或隱含)。請閱讀Interfaces (C# Programming Guide)

interface IFoo 
{ 
    void FooMethod(); 
} 

class Foo : IFoo 
{ 
    public Foo() { } 

    public void FooMethod() 
    { 
     //actual IFoo implementation by Foo 
    } 
} 
+0

+1好,這不是我見過的第一個問題,我想知道他們在哪裏得到所有無關緊要的東西。 –