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 / Windows Forms / WinForm Data Binding / October 2004

Tip: Looking for answers? Try searching our database.

StackOverflowException with the ListChanged event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alx Sharp - 13 Oct 2004 00:06 GMT
Hello group...

I've created a collection class that implements the following interfaces: IBindingList, IList, ICollection, IEnumerable and ITypedList:

abstract class DataCollectionBase : IBindingList, IList, ICollection, IEnumerable, ITypedList
{
   private ArrayList innerList = null;
   private System.Type itemType = null;

   public DataCollectionBase()
   {
       this.innerList = new ArrayList();
   }

   protected System.Type ItemType
   {
       get { return this.itemType; }
       set { this.itemType = value; }
   }

   // List Changed Notification
   protected virtual void OnInsertComplete(int index, object newValue)
   {
       if (ListChanged != null)
       {
           ListChangedEventArgs args = new ListChangedEventArgs(ListChangedType.ItemAdded, index, -1);
           ListChanged(this, args);        // The StackOverflowException is thrown here
       }
   }

   // IList Implementation
   int IList.Add(object value)
   {
       int index = this.innerList.Add(value);
       OnInsertComplete(index, value);
   }

   // IBindingList Implementation
   public event ListChangedEventHandler ListChanged = null;

   object IBindingList.AddNew()
   {
       object newItem = null;

       // Here I create the new item with Reflection, based on the ItemType property and store a reference in 'newItem''.

       ((IList)this).Add(newItem);
       return newItem;
   }
}

I'm using my collection as a value for the DataSource property in a DataGrid control.
The IBindingList.AddNew() method works fine and the new item is instantiated via Reflection, but when I invoke the ListChanged event handler within the OnInsertComplete(..) method, the StackOverflowException is thrown.
I don't understand what's happening to my collection, the DataGrid control is the only component that's consuming the ListChanged event.

I really need your help...

Thank you all
Sijin Joseph - 13 Oct 2004 09:31 GMT
Set a break point on OnInsertComplete() once your  code reaches the
event firing, see if OnInsertComplete is being called recursively..

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> Hello group...
>  
[quoted text clipped - 64 lines]
>  
> Thank you all...
Alx Sharp - 13 Oct 2004 17:45 GMT
Hi...

I already did that... I ran my program in debug mode and I went step by step
into every method call, but the exception is thrown the first time the
OnInsertComplete(..) method is invoked.

protected virtual void OnInsertComplete(int index, object value)
{
   if (ListChanged != null)
   {
       ListChangedEventArgs args = new
ListChangedEventArgs(ListChangedType.ItemAdded, index, -1);
       ListChanged(this, args); // The StackOverflowException is thrown
here
   }
}

Thank you...

> Set a break point on OnInsertComplete() once your  code reaches the event
> firing, see if OnInsertComplete is being called recursively..
[quoted text clipped - 58 lines]
>>  I really need your help...
>>  Thank you all...
Alx Sharp - 13 Oct 2004 18:10 GMT
Hello again...

If I add/remove items to or clear my collection before I use it as a data
source for the DataGrid control it works fine, actually, I'm consuming the
ListChanged event from the form the DataGrid is in, and I can show a
MessageBox every time the collection changes. The problem occurs when I set
the DataSource property to my collection, because the StackOverflowException
is thrown.

Thank you... and excuse me if I insist, but I really need this for an
important project...

> Set a break point on OnInsertComplete() once your  code reaches the event
> firing, see if OnInsertComplete is being called recursively..
[quoted text clipped - 58 lines]
>>  I really need your help...
>>  Thank you all...
Alx Sharp - 13 Oct 2004 18:35 GMT
Hello and thank you all

I've found the problem... this is my item class named EditableObject:

class EditableObject : IEditableObject
{
   protected bool isUndefined;
   protected bool isEditing;

   public EditableObject()
   {
       this.isUndefined = true;
       this.isEditing = false;
   }

   // IEditableObject interface
   public void BeginEdit()
   {
       if (!this.IsUndefined)
       {
           if (this.innerRow != null)
           {
               this.innerRow.BeginEdit();
           }
           this.isEditing = true;
           this.isUndefined = false;
       }
   }

   public bool IsEditing
   {
       get { return this.isEditing; }
   }

   public bool IsUndefined
   {
       get { return this.IsUndefined; } // Here's the problem, IsUndefined
instead of isUndefined (StackOverflowException) : )
   }
}

When the collection is set as the data source for the DataGrid control and
the datagrid is focused, a new item is added and the BeginEdit() method
invoked.
This is a problem very hard to find, because I was focusing my atention to a
totally diferent part of my program...

Thank you all...

> Set a break point on OnInsertComplete() once your  code reaches the event
> firing, see if OnInsertComplete is being called recursively..
[quoted text clipped - 58 lines]
>>  I really need your help...
>>  Thank you all...
Sijin Joseph - 14 Oct 2004 04:43 GMT
Cheers :)

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> Hello and thank you all
>
[quoted text clipped - 107 lines]
>>> I really need your help...
>>> Thank you all...

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.