Tuesday, December 9, 2008

Using CollectionBase

Sample code for System.Collections.CollectionBase implementaion.
public class Attributes : CollectionBase
{
private string _key;
private string _value;

public string Key
{
get { return this._key; }
set { this._key = value; }
}

public string Value
{
get { return this._value; }
set { this._value = value; }
}

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

public string this[string Key]
{
get
{
string returnValue = "";
if (this.Count > 0)
{
foreach (Attributes attr in this)
{
if (attr.Key.ToLower().Equals(Key.ToLower()))
{
returnValue = attr.Value;
break;
}
}
}
return returnValue;
}
}

public bool Contains(Attributes attribute)
{
return List.Contains(attribute);
}

public int Add(Attributes attribute)
{
return List.Add(attribute);
}

public void Insert(int index, Attributes attribute)
{
List.Insert(index, attribute);
}

public void Remove(Attributes attribute)
{
List.Remove(attribute);
}

public new void RemoveAt(int index)
{
List.RemoveAt(index);
}
}

1 comment:

Unknown said...

Nice article was useful..