Problems with custom collection (CollectionBase implementation)

G

Guest

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!
 
D

Dan Bass

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.
 
G

Guest

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!
 
G

Guest

Hello again,

I solved the problem. I added a "set" accessor to the Units AND prevented
the "get" from resetting row.Units to whatever is in the DB every time it is
called. Stupid, stupid me.

Sorry for the inconvenience!

/Jannik
 
D

Dan Bass

glad you sorted it out.

Jannik Anker said:
Hello again,

I solved the problem. I added a "set" accessor to the Units AND prevented
the "get" from resetting row.Units to whatever is in the DB every time it
is
called. Stupid, stupid me.

Sorry for the inconvenience!

/Jannik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top