Jump to content
  • Advertisement
Sign in to follow this  
Barguast

[.net] Collections problem

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been messing around with 'collection' properties in C#. The problem is that while I can create a collection property and access the Collection Editor, the items that I put into the collection aren't saved - when I go back to the collection editor then all of the objects I originally put in there are gone. I've done a very simple example here to demonstrate how I'm going about this. Hopefully someone can point out what I'm doing wrong. The following is a UserControl which has a collection property which can contain integers.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace Test_Collection
{
	#region IntList

	public class IntList : System.Windows.Forms.UserControl
	{
		private System.ComponentModel.Container components = null;
		private IntCollection ints = null;

		#region Constructors / Dispose

		// Default Constructor
		public IntList ()
		{
			InitializeComponent();
		}

		// Dispose
		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();
			}
			base.Dispose (disposing);
		}

		#endregion

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// ButtonColumn
			// 
			this.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.Name = "ButtonColumn";
			this.Size = new System.Drawing.Size(160, 152);

		}
		#endregion

		#region Properties

		public IntCollection Numbers
		{
			get { return ints; }
			set { ints = value; }
		}

		#endregion
	}

	#endregion

	#region IntCollection

	public class IntCollection : CollectionBase
	{
		// Default Constructor
		public IntCollection()
		{

		}

		#region Overrided collection functions

		public void Add (int integer)
		{
			this.List.Add (integer);
		}

		public void Remove (int index)
		{
			if ((index < 0) || (index >= this.List.Count))
				throw new IndexOutOfRangeException ("Invalid index");

			this.List.RemoveAt (index);
		}

		public int IndexOf (int integer)
		{
			return this.List.IndexOf (integer);
		} 

		public int this [int index]
		{
			get
			{
				if ((index < 0) || (index >= this.List.Count))
					throw new IndexOutOfRangeException ("Invalid index");

				return (int) this.List [index];
			}
		}

		#endregion
	}

	#endregion
}

Try adding this control to a form and then add some numbers to the Numbers property with the collection editor. If you then go back to this property then all of the numbers have gone - I can't work out why they are disappearing. Hope someone can help!

Share this post


Link to post
Share on other sites
Advertisement
This is just off the top of my head , but instead of using 'this.List' try using 'this.innerlist'


Share this post


Link to post
Share on other sites
Try this:

Call CollectionBase's constructor in your constructor. If the base class needs some initializations, this may help.

This is the code:

public IntList () : base ()
...

Share this post


Link to post
Share on other sites
I presume you meant the base class of IntCollection, not IntList?

Regardless, it doesn't make any difference if I called the base construction in either IntList or IntCollection.

Any more ideas? (Again!)

EDIT: I worked it out - for some reason the InitializeComponent part had messed up (as you can see in the code) and as a result, the 'ints' variable is never initialised

[Edited by - Barguast on August 18, 2004 8:06:52 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!