Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:06+00:00 2026-05-25T16:22:06+00:00

I have a class that contains a list of objects. I previously had this

  • 0

I have a class that contains a list of objects.

I previously had this as type of IList which mapped fine. However, I was wanting to add the ability to add/remove/edit items in this List via a PropertyGrid control.

I was therefore required to set the List as a type of Collection derived from CollectionBase and that contained a ICustomTypeDescriptor in order for this to work rather than the original IList.

I was wandering if someone could tell me how, or if it will be possible to map this list using this method, or how I can modify my current approach to get the items in the list to be editable via the PropertyGrid.

Here is my CollectionClass, which I’d also like to figure out how to make generic so I could reuse this, but nevertheless here are the two classes which I had to define in order to make the List of objects editable via the PropertyGrid:

public class ZoneCollection : CollectionBase, ICustomTypeDescriptor
{
    #region Collection Implementation

    /// <summary>
    /// Adds an zone object to the collection
    /// </summary>
    /// <param name="emp"></param>
    public void Add(Zone zone)
    {
        this.List.Add(zone);
    }

    /// <summary>
    /// Removes an zone object from the collection
    /// </summary>
    /// <param name="emp"></param>
    public void Remove(Zone zone)
    {
        this.List.Remove(zone);
    }

    /// <summary>
    /// Returns an employee object at index position.
    /// </summary>
    public Zone this[int index]
    {
        get
        {
            return (Zone)this.List[index];
        }
    }

    #endregion

    // Implementation of interface ICustomTypeDescriptor 
    #region ICustomTypeDescriptor impl

    public String GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    public String GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(this, true);
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }

    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }


    /// <summary>
    /// Called to get the properties of this type. Returns properties with certain
    /// attributes. this restriction is not implemented here.
    /// </summary>
    /// <param name="attributes"></param>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }

    /// <summary>
    /// Called to get the properties of this type.
    /// </summary>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties()
    {
        // Create a collection object to hold property descriptors
        PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);

        // Iterate the list of employees
        for (int i = 0; i < this.List.Count; i++)
        {
            // Create a property descriptor for the employee item and add to the property descriptor collection
            ZoneCollectionPropertyDescriptor pd = new ZoneCollectionPropertyDescriptor(this, i);
            pds.Add(pd);
        }
        // return the property descriptor collection
        return pds;
    }

    #endregion
}

/// <summary>
/// Summary description for CollectionPropertyDescriptor.
/// </summary>
public class ZoneCollectionPropertyDescriptor : PropertyDescriptor
{
    private ZoneCollection collection = null;
    private int index = -1;

    public ZoneCollectionPropertyDescriptor(ZoneCollection coll, int idx) :
        base("#" + idx.ToString(), null)
    {
        this.collection = coll;
        this.index = idx;
    }

    public override AttributeCollection Attributes
    {
        get
        {
            return new AttributeCollection(null);
        }
    }

    public override bool CanResetValue(object component)
    {
        return true;
    }

    public override Type ComponentType
    {
        get
        {
            return this.collection.GetType();
        }
    }

    public override string DisplayName
    {
        get
        {
            Zone zone = this.collection[index];
            return zone.ID.ToString();
        }
    }

    public override string Description
    {
        get
        {
            Zone zone = this.collection[index];
            StringBuilder sb = new StringBuilder();
            sb.Append(zone.ID.ToString());

            if ( zone.Streets.Route != String.Empty || zone.Streets.Crossing != String.Empty)
                sb.Append("::");
            if (zone.Streets.Route != String.Empty)
                sb.Append(zone.Streets.Route);
            if ( zone.Streets.Crossing != String.Empty)
            {
                sb.Append(" and ");
                sb.Append(zone.Streets.Crossing);
            }

            return sb.ToString();
        }
    }

    public override object GetValue(object component)
    {
        return this.collection[index];
    }

    public override bool IsReadOnly
    {
        get { return false; }
    }

    public override string Name
    {
        get { return "#" + index.ToString(); }
    }

    public override Type PropertyType
    {
        get { return this.collection[index].GetType(); }
    }

    public override void ResetValue(object component)
    {
    }

    public override bool ShouldSerializeValue(object component)
    {
        return true;
    }

    public override void SetValue(object component, object value)
    {
        // this.collection[index] = value;
    }
}

Here is my Zone class:

[ComVisible(true)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[CategoryAttribute("Configuration")]
[Serializable]
public class Zone
{
    #region Private Fields

    private bool active;
    private string dir;
    private Heading heading = new Heading();
    private int id;
    private int intID;
    private Position start = new Position();
    private Position finish = new Position();
    private int width;
    private Position[] corners = new Position[4];
    private Streets streets = new Streets();

    #endregion        

    #region Constructors

    public Zone() 
    {
        if (Program.main != null)
        {
            IntID = Program.main.intID;

            Intersection intersection = Program.data.Intersections.list.Find(
                delegate(Intersection tInt)
                {
                    return tInt.ID == IntID;
                }
            );

            if (intersection != null)
            {
                Streets.Crossing = intersection.Streets.Crossing;
                Streets.Route = intersection.Streets.Route;
            }
        }
    }

    #endregion

    #region Properties

    public virtual long PK { get; set; }

    [Browsable(false)]
    public virtual bool Active
    {
        get { return active; }
        set { active = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The direction for the Zone.")]
    public virtual string Dir
    {
        get { return dir; }
        set { dir = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The heading for the Zone.")]
    public virtual Heading Heading
    {
        get { return heading; }
        set { heading = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The Zone Identification Number.")]
    public virtual int ID
    {
        get { return id; }
        set { id = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The Identification Number associated with the Priority Detector of the Zone.")]
    public virtual int IntID
    {
        get { return intID; }
        set { intID = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The location of the Zone's Start.")]
    public virtual Position Start
    {
        get { return start; }
        set { start = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The location of the Zone's Finish.")]
    public virtual Position Finish
    {
        get { return finish; }
        set { finish = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The width of the Zone.")]
    public virtual int Width
    {
        get { return width; }
        set { width = value; }
    }

    [Browsable(false)]
    public virtual Position[] Corners
    {
        get { return corners; }
        set { corners = value; }
    }

    [CategoryAttribute("Configuration"),
        DescriptionAttribute("The streets associated with the Zone."),
        DisplayName("Zone Streets")]
    public virtual Streets Streets
    {
        get { return streets; }
        set { streets = value; }
    }

    #endregion
}

And here is my original mapping of the list of the Zone class objects contained within my Intersection class:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Devices.Device, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Device`" lazy="false">
  <id name="PK" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <column name="PK" />
    <generator class="identity" />
  </id>
  <many-to-one class="EMTRAC.Connections.Connection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="LocalConnection" lazy="false" cascade="all">
    <column name="LocalConnection_id" />
  </many-to-one>
  <many-to-one class="EMTRAC.Connections.Connection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Connection" lazy="false" cascade="all">
    <column name="Connection_id" />
  </many-to-one>
  <many-to-one class="EMTRAC.Packets.Packet, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Configuration" lazy="false" cascade="all">
    <column name="Configuration_id" />
  </many-to-one>
  <joined-subclass name="EMTRAC.Intersections.Intersection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" lazy="false">
    <key>
      <column name="Device_id" />
    </key>
    <bag name="Zones" cascade="all-delete-orphan">
      <key>
        <column name="Intersection_id" />
      </key>
      <one-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </bag>
    <many-to-one class="EMTRAC.Intersections.Streets, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Streets" lazy="false" cascade="all">
      <column name="Streets_id" />
    </many-to-one>
    <many-to-one class="EMTRAC.Positions.Position, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Position" lazy="false" cascade="all">
      <column name="Position" />
    </many-to-one>
  </joined-subclass>
</class>

I originally went with a simple bag b/c I was using an IList, but I’m not sure what or how I would do this now that the Zones list is not an IList but rather a ZoneCollection class that derrives from CollectionBase.

I assume it is puking b/c I don’t have the ZoneCollection class mapped out, but I don’t know how to begin mapping this b/c all the class there is is a list of zones objects. Do I just map the class and have a bag inside that class?

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:22:07+00:00Added an answer on May 25, 2026 at 4:22 pm

    there are some options

    1: implementing a NH custom collection

    2: use an inner collection in your own collection

    for example

    class ZoneCollection : IList<Zone>, ICustomTypeDescriptor
    {
        //  Must be defined as an IList and not a List for NHibernate to save correctly
        private IList<Zone> _inner;
    
        public ZoneCollection()
        {
            _inner = new List<Zone>();
        }
    
        public int IndexOf(Zone item)
        {
            return _inner.IndexOf(item);
        }
    
        // ...
    }
    
    <component name="Zones" access="nosetter.camelcase-underscore">
      <bag name="_inner" access="field" cascade="all-delete-orphan">
        <key>
          <column name="Intersection_id" />
        </key>
        <one-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bag>
    </component>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
I have a class Player that contains a list of Accessory objects. There are
I have a Ticket class that contains a List of User objects: class Ticket
I have a class that contains a static number of objects. This class needs
Problem: I have a document class which contains a list of objects. These objects
I have a class that contains a list of objects. What's the best way
I have a ViewModel class that contains a list of points, and I am
I have a class that contains data from some model. This class has metadata
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have a class that contains a boolean field like this one: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.