Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Problem with custom collections.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wade - 21 Mar 2006 07:29 GMT
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);
Wade - 21 Mar 2006 07:39 GMT
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!

> Hi,
>
[quoted text clipped - 140 lines]
>    Polygons htPolygons = new Polygons();
>    htPolygons.Add(alCurrentPolygonPoints);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.