473,464 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with custom collection (CollectionBase implementation)

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!
Nov 16 '05 #1
4 1373

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!

Nov 16 '05 #2
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!


Nov 16 '05 #3
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

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


Nov 16 '05 #4
glad you sorted it out.

"Jannik Anker" <Ja*********@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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

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


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: GrumpyDev | last post by:
what is the best way to implement collection of custom entities?
0
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
0
by: Thomas D. | last post by:
Situation: --------------------------- I have an 'export'-wrapper to my regular objects. For each regular object there is also an export object. An export object derives from the regular object...
2
by: Ken Varn | last post by:
I want to create a custom collection class that is similar to something like the System.Windows.Forms.Control.ControlCollection. I notice that this collection implements IList, ICollection,...
5
by: aa7im | last post by:
I am attempting to create a base collection that I can use to derive strongly typed collection. My base collection class will derive from CollectionBase and I want my typed collections to be...
8
by: Tinus | last post by:
Hello all, I've create a custom control (UserControl) and have a custom Item Collection. The control is a custom calendar which is draw using the Graphics Rectangle etc. functions. It is drawn...
2
by: A Traveler | last post by:
Hi, I have a custom collection class i wrote, LineItemsCollection, which is a strongly typed collection of objects of my LineItem class. The LineItem class is a simple class with just a couple...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.