2014-06-19 50 views
0

我有一些麻煩,使索引器與對象列表一起工作。我希望能夠通過它的名字(一個字符串)而不是索引來訪問列表中的每個對象。所以,我想重載[]運算符來實現這一點。到目前爲止,我無法在intellisense中看到超負荷現象,並且此時不起作用。我至今是:過載支架[]運營商的列表<object>

所謂的MapInfo一類的單一對象:

MapInfo mi = MapInfo.Instance; 

在MapInfo的類我有表對象的列表:

List<Table> tables; 
    public List<Table> Tables 
    { 
     get { return tables; } 
    } 

最後表類我試過這個:

class Table : IEnumerable<Table> 
{ 

    public Table this[string tableName] 
    { 
     get 
     { 
      foreach (Table table in this) 
      { 
       if (table.name == tableName) 
        return table; 
      } 
      return null; 
     } 
    } 

我想能夠訪問我的表對象使用:

mi.Tables["SomeTableName"] 

這是我第一次嘗試這個,所以我不太確定我要出錯的地方。

+0

「此刻不起作用」。出了什麼問題?你有錯誤嗎?發生了什麼,你不指望? –

+0

對不起,有點不清楚。索引器沒有被重載。我無法看到intellisense彈出窗口中的重載,如果嘗試使用name屬性訪問索引,它會出現語法錯誤。 –

+0

'Tables'是'List

',它沒有字符串索引器。它看起來像你需要創建自己的集合類型,並改變'MapInfo.Tables'的類型來取而代之。 – Lee

回答

1

你可以使用的方法是這樣

public class MapInfo { 
    private readonly TableCollection _tables = new TableCollection(); 

    public TableCollection Tables { 
     get { return _tables; } 
    } 
} 

public class TableCollection : List<Table> { 
    public Table this[string name] { 
     get { return this.FirstOrDefault(t => t.Name == name); /*using System.Linq;*/ } 
    } 
} 

public class Table { 
    public string Name { get; set; } 
} 

或者乾脆使用字典(Dictionary<string, Table>)作爲Danaldo建議。但他們都沒有,因爲他編碼=))

國際海事組織,正確的方法是不使用像這些索引,因爲我看到可以有多個表中的'獨特'名稱在您的集合。我推薦使用表的簡單列表,並像一個GetTableWithName方法來讓事情更清晰,因爲索引通常給出一個(假)希望你的數據是唯一

或者你可以用SingleOrDefault替換到FirstOrDefault呼叫這將在內部確保如果存在具有「名稱」的元素,則其他元素不具有相同的「名稱」

1

您正在重載Table類上的索引器。你的代碼結構出錯了。 Table類是IEnumerable<Table>,所以表包含其他表。

因此,List<Table>將包含Table實例,該實例又包含Table實例。

With mi.Tables["SomeTableName"]您試圖訪問List的索引器,而不是Table的索引器。

爲什麼不在MapInfo中定義索引器?

1

使用字典

Private Dictionary<String, Table> tables; 
public Dictionary<String, Table> Tables 
{ 
    get { return tables; } 
} 

然後:

class Table : IEnumerable<Table> 
{ 
    public Table this[string tableName] 
    { 
     get 
     { 
      Table table; 
      if(mi.tables.TryGetValue(tableName, out table)) 
      { 
       return table; 
      } 
      else 
      { 
       return null; 
      } 
     } 
    } 
} 
0

您的MapInfo/Table的實現並不完全正確,無法讓您按自己的意願進行操作。

您在MapInfo中表的屬性不能是表的列表,它必須是Table類型。然後你的Table類應該完全實現IEnumerable接口。 使用LinqPad編寫的快速示例:

class Table : IEnumerable<Table> 
{ 
    public Table() 
    { 
     _tables = new Dictionary<string, Table>(); 
    } 

    public string name { get; set; } 
    Dictionary<string, Table> _tables; 
    public void Add(string tname) 
    { 
     _tables.Add(tname, new Table { name = tname }); 
    } 
    public Table this[string tableName] 
    { 
     get 
     { 
      Table table; 
      if (_tables.TryGetValue(tableName, out table)) 
       return table; 
      return null; 
     } 
    } 


    public IEnumerator<Table> GetEnumerator() 
    { 
     return _tables.Values.GetEnumerator(); 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.GetEnumerator(); 
    } 
} 


void Main() 
{ 
    MapInfo mi = new MapInfo(); 
    mi.Table["foo"].Dump(); 
} 
class MapInfo 
{ 
    public MapInfo() 
    { 
     Tables = new Table(); 
     test(); 
    } 
    private void test() 
    { 
     Tables.Add("foo"); 
     Tables.Add("bar"); 
     Tables.Add("soom"); 
    } 
    public Table Tables { get; set; } 
}