Hello Daniel,
Okay, when I said readonly, I meant that the Units property only has a "get"
accessor. No actual use of the keyword though:
public UnitCollection Units
{
get
{
UnitsFromDatabase u = new UnitsFromDatabase(this.Id);
return u.All(); // returns all units for current row
}
}
This is my entire UnitCollection implementation:
public class UnitCollection : CollectionBase
{
public void Add(Unit _unit)
{
List.Add(_unit);
}
public void Insert(int index, Unit _unit)
{
List.Insert(index, _unit);
}
public bool Contains(Unit _unit)
{
return List.Contains(_unit);
}
public int IndexOf(Unit _unit)
{
return List.IndexOf(_unit);
}
public void CopyTo(Unit[] array, int index)
{
List.CopyTo(array, index);
}
public void Remove(int index)
{
if((index > (Count - 1)) || (index < 0))
throw( new Exception("Invalid index") );
List.RemoveAt(index);
}
public void Remove(Unit _unit)
{
if(List.Contains(_unit))
List.Remove(_unit);
else
throw( new Exception("Row was not found") );
}
public virtual Unit this[int index]
{
get
{
return (Unit) this.List[index];
}
}
}
Thanks for your help!
"Dan Bass" wrote:
the "readonly" keyword stops you from changing the variable value, but if
you have an object which isn't immutable, then it can be changed.
I.E.
// units changes because the part doing the
// alteration exists in the Units object
row.Units.Add ( myNewUnit );
// compiler error... on 2nd line (CS0191)
UnitCollection otherUnits = new UnitCollection();
row.Units = otherUnits;
As to why your collection isn't being incremented, the code provided doesn't
really point to where the problem may be. Could you give us more of the
UnitCollection class?
Thanks.
Daniel.
"Jannik Anker" <Ja*********@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...I have a standard CollectionBase implementation called UnitCollection. And
yes, the collection items are class Unit ;-)
Code:
public class Row
{
public Row()
{ ... }
public UnitCollection Units
{
get
{
...
}
}
}
Problem occurs when I try to do this:
Row row = new Row();
row.Units.Add(new Unit());
foreach(Unit u in row.Units)
{
...
}
The UnitCollection (Units) has no items... I'm wondering whether it has
something to do with the fact that Units is a readonly property of class
Row?
Stupid question, I guess, but I don't know what to do...
Hope I've made an understandable point. And that someone can help,
naturally!