Problem with custom collections.

  • Thread starter Thread starter Wade
  • Start date Start date
W

Wade

Hi,

I have used the collectionbase to create two different collections. One
various button clicks, I add values into these collections. I'm having a
problem, though, for as soon as I add more than one item to the collection,
I am only able to retrieve the most recent item I've added. For example,
when I debug, and look at the collectionbase list, I only see the "raw
value" for all my items, except for the most recently added item.

Any thoughts?

Note: I am running this on the Compact Framework 2.0 -- from what I've seen,
though, there shouldn't be anything different from the regular Framework
2.0.

Here is the code I'm using:

public class PolygonPoints : CollectionBase
{
public PolygonPoints()
{
}

public PolygonPoints(PolygonPoints coll)
{
this.InnerList.AddRange(coll);
}

public Point this[int index]
{
get
{
return ((Point)List[index]);
}
set
{
List[index] = value;
}
}
public virtual void Add(Point item)
{
List.Add(item);
}
public virtual void Insert(int index, Point item)
{
List.Add(item);
}
public virtual void Remove(Point item)
{
List.Remove(item);
}
public bool Contains(Point item)
{
return List.Contains(item);
}
public int IndexOf(Point item)
{
return List.IndexOf(item);
}
public void CopyTo(Point[] array, int index)
{
List.CopyTo(array, index);
}

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is Point))
{
throw new ArgumentException("PolygonPoints only supports
Point objects.");
}
}

}

public class Polygons : CollectionBase
{
public Polygons()
{
}

public Polygons(Polygons coll)
{
this.InnerList.AddRange(coll);
}

public PolygonPoints this[int index]
{
get
{
return ((PolygonPoints)List[index]);
}
set
{
List[index] = value;
}
}
public virtual void Add(PolygonPoints item)
{
List.Add(item);
}
public virtual void Insert(int index, PolygonPoints item)
{
List.Add(item);
}
public virtual void Remove(PolygonPoints item)
{
List.Remove(item);
}
public bool Contains(PolygonPoints item)
{
return List.Contains(item);
}
public int IndexOf(PolygonPoints item)
{
return List.IndexOf(item);
}
public void CopyTo(PolygonPoints[] array, int index)
{
List.CopyTo(array, index);
}

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is PolygonPoints))
{
throw new ArgumentException("Polygons only supports
PolygonPoints objects.");
}
}
}

Here is how I am adding data into my collections:

PolygonPoints alCurrentPolygonPoints = new PolygonPoints();
Point arrayPt = new Point(1,1);
alCurrentPolygonPoints.Add(arrayPt);

And:

Polygons htPolygons = new Polygons();
htPolygons.Add(alCurrentPolygonPoints);
 
I finally figured it out ...

As I added the PolygonPoints to my Polygons, I wasn't creating a new
instance. I should have done something like this:

PolygonPoints tmp = new PolygonPoints(alPolygonPoints);
htPolygons.Add(tmp);

Ugh!

Wade said:
Hi,

I have used the collectionbase to create two different collections. One
various button clicks, I add values into these collections. I'm having a
problem, though, for as soon as I add more than one item to the
collection, I am only able to retrieve the most recent item I've added.
For example, when I debug, and look at the collectionbase list, I only see
the "raw value" for all my items, except for the most recently added item.

Any thoughts?

Note: I am running this on the Compact Framework 2.0 -- from what I've
seen, though, there shouldn't be anything different from the regular
Framework 2.0.

Here is the code I'm using:

public class PolygonPoints : CollectionBase
{
public PolygonPoints()
{
}

public PolygonPoints(PolygonPoints coll)
{
this.InnerList.AddRange(coll);
}

public Point this[int index]
{
get
{
return ((Point)List[index]);
}
set
{
List[index] = value;
}
}
public virtual void Add(Point item)
{
List.Add(item);
}
public virtual void Insert(int index, Point item)
{
List.Add(item);
}
public virtual void Remove(Point item)
{
List.Remove(item);
}
public bool Contains(Point item)
{
return List.Contains(item);
}
public int IndexOf(Point item)
{
return List.IndexOf(item);
}
public void CopyTo(Point[] array, int index)
{
List.CopyTo(array, index);
}

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is Point))
{
throw new ArgumentException("PolygonPoints only supports
Point objects.");
}
}

}

public class Polygons : CollectionBase
{
public Polygons()
{
}

public Polygons(Polygons coll)
{
this.InnerList.AddRange(coll);
}

public PolygonPoints this[int index]
{
get
{
return ((PolygonPoints)List[index]);
}
set
{
List[index] = value;
}
}
public virtual void Add(PolygonPoints item)
{
List.Add(item);
}
public virtual void Insert(int index, PolygonPoints item)
{
List.Add(item);
}
public virtual void Remove(PolygonPoints item)
{
List.Remove(item);
}
public bool Contains(PolygonPoints item)
{
return List.Contains(item);
}
public int IndexOf(PolygonPoints item)
{
return List.IndexOf(item);
}
public void CopyTo(PolygonPoints[] array, int index)
{
List.CopyTo(array, index);
}

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is PolygonPoints))
{
throw new ArgumentException("Polygons only supports
PolygonPoints objects.");
}
}
}

Here is how I am adding data into my collections:

PolygonPoints alCurrentPolygonPoints = new PolygonPoints();
Point arrayPt = new Point(1,1);
alCurrentPolygonPoints.Add(arrayPt);

And:

Polygons htPolygons = new Polygons();
htPolygons.Add(alCurrentPolygonPoints);
 
Back
Top