代码改变世界

CollectionBase的使用

2011-08-05 18:57  Carl Xing  阅读(341)  评论(0编辑  收藏  举报
    interface IItem
{
object name { get; set; }
object value { get; set; }
void getItemByName();
}

public class ItemClass : IItem
{
#region IItem 成员

public object name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public object value
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public void getItemByName()
{
throw new NotImplementedException();
}

#endregion
}
public class ItemCollection : CollectionBase
{
public void Add(ItemClass item)
{}

public ItemClass this[int i]
{
get
{
return this.InnerList[i] as ItemClass;
}
}

public ItemClass this[string name]
{
get
{
ItemClass reItem
= null;
ArrayList lists
= this.InnerList;
for (int i = 0; i < lists.Count; i++)
{
ItemClass item
= lists[i] as ItemClass;
if (string.Compare(name, item.name.ToString()) == 0)
{
reItem
= item;
}
}
return reItem;
}
}
}

这样就可以通过索引或键值来找到对应的item了。