473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StackOverflowException with the ListChanged event

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...
Nov 16 '05 #1
11 2867
Can you show us the ListChanged handler? StackOverflow => some (unintended,
unanticipated) loop that adds something with each iteration?
Nov 16 '05 #2
Hello Joep...

The ListChangedEventHandler is part of the .NET Framework and it's declared
under the System.ComponentModel namespace as follows:

public delegate void ListChangedEventHandler(object sender,
ListChangedEventArgs e);

and the IBindingList interface includes an event of that type.

interface IBindingList : IList, ICollection, IEnumerable
{
// Properties go here

event ListChangedEventHandler ListChanged;

// Methods go here
}

So, when my class implements the IBindingList interface I just have to write
something like this:

class DataCollectionBase : IBindingList, IList, ICollection, IEnumerable,
ITypedList
{
public event ListChangedEventHandler ListChanged = null;

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
}
}
}

As you can see, (from my point of view) I'm not doing anything strange with
the event.
Only the DataGrid control consumes the event behind the scenes with
something like:

((IBindingList)DataSource).ListChanged += new
ListChangedEventHandler(some_method);

At least that's what I think, please right me if I'm wrong...

Thank you all again...

"Joep" <St***@DeStoep.nl> escribió en el mensaje
news:41*********************@news.xs4all.nl...
Can you show us the ListChanged handler? StackOverflow => some
(unintended, unanticipated) loop that adds something with each iteration?

Nov 16 '05 #3
Could you perhaps provide the stack trace and, if possible, a short but
complete program[1] that exhibits your issue?
Its possible that there is something else in your application that is
causing the issue. Whittle down your code until it is nothing but the code
required to cause the problem. Although, contrary to the article, in this
case you would provide a GUI.

1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet, another
C# MVP. I mention it only because the page itself is written in first person
and doesn't seem to mention who the author is.)
Nov 16 '05 #4
Hello Daniel...

The Stack Trace:

at System.Windows.Forms.DataGrid.AddNewRow()
at System.Windows.Forms.DataGridAddNewRow.OnEdit()
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocuse dControl()
I just wrote this line in my main form constructor, after the
InitializeComponent() call:

this.dataGrid1.DataSource = myCollection; // myCollection is of a class
derived from DataCollectionBase, which I've described before...
// Right here the StackOverflowException is thrown.
// I ran my application in Debug mode and I went step by step into every
method call until I reached the ListChanged(..) invocation (the origin of
the exception).

Thank you for your help...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> escribió en el
mensaje news:eb**************@TK2MSFTNGP12.phx.gbl...
Could you perhaps provide the stack trace and, if possible, a short but
complete program[1] that exhibits your issue?
Its possible that there is something else in your application that is
causing the issue. Whittle down your code until it is nothing but the code
required to cause the problem. Although, contrary to the article, in this
case you would provide a GUI.

1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet,
another C# MVP. I mention it only because the page itself is written in
first person and doesn't seem to mention who the author is.)

Nov 16 '05 #5
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
Could you perhaps provide the stack trace and, if possible, a short but
complete program[1] that exhibits your issue?
Its possible that there is something else in your application that is
causing the issue. Whittle down your code until it is nothing but the code
required to cause the problem. Although, contrary to the article, in this
case you would provide a GUI.
Not quite contrary to the article - it only says to remove GUI aspects
if they're unnecessary. A question *about* a GUI is fine to include a
GUI, of course - although it should be as pared down as possible.
1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet, another
C# MVP. I mention it only because the page itself is written in first person
and doesn't seem to mention who the author is.)


That's a good point. Would you like me to rewrite it in the third
person, or perhaps put a "who wrote this" bit at the start?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
Could you perhaps provide the stack trace and, if possible, a short but
complete program[1] that exhibits your issue?
Its possible that there is something else in your application that is
causing the issue. Whittle down your code until it is nothing but the
code
required to cause the problem. Although, contrary to the article, in this
case you would provide a GUI.


Not quite contrary to the article - it only says to remove GUI aspects
if they're unnecessary. A question *about* a GUI is fine to include a
GUI, of course - although it should be as pared down as possible.


Ya ya, I know. Don't need to be semantically precise do we? :)

My reading of the article suggests avoiding a GUI where possible and I just
wanted to make it clear that it is entirely appropriate here(although *it*
might have been possible to prove the bug without a GUI, atleast until I saw
the stack trace. I think its unlikely at this point). Although I could have
pointed it out in a different way.

Sorry for implying an error in your article, however, ;)
1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet,
another
C# MVP. I mention it only because the page itself is written in first
person
and doesn't seem to mention who the author is.)


That's a good point. Would you like me to rewrite it in the third
person, or perhaps put a "who wrote this" bit at the start?


Being in the first person is fine, but I think atleast some accreditation is
in order. Something as simple as a "By Jon Skeet" would suffice nicely.
I don't want to appear to be claiming your work, afterall, ;).

One other point, however. You might want to suggest, for GUI related
questions, that the author take the extra step and avoid using the forms
designer(or any designer, for that matter, except where the designer is in
question). Right now its not too big of a deal, however, with the coming of
partial classes the forms designer is going to be one PITA to copy and paste
in. I worry we'll start to see
public partial class MyClass
{
//...
}
public partial class MyClass
{
private void InitalizeComponent()
{
//...
}
}
or simply two attached files.
Nov 16 '05 #7
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
Not quite contrary to the article - it only says to remove GUI aspects
if they're unnecessary. A question *about* a GUI is fine to include a
GUI, of course - although it should be as pared down as possible.
Ya ya, I know. Don't need to be semantically precise do we? :)

My reading of the article suggests avoiding a GUI where possible and I just
wanted to make it clear that it is entirely appropriate here(although *it*
might have been possible to prove the bug without a GUI, atleast until I saw
the stack trace. I think its unlikely at this point). Although I could have
pointed it out in a different way.

Sorry for implying an error in your article, however, ;)


No problem. I'll amend the article to make it clearer.
1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet,
another
C# MVP. I mention it only because the page itself is written in first
person
and doesn't seem to mention who the author is.)


That's a good point. Would you like me to rewrite it in the third
person, or perhaps put a "who wrote this" bit at the start?


Being in the first person is fine, but I think atleast some accreditation is
in order. Something as simple as a "By Jon Skeet" would suffice nicely.
I don't want to appear to be claiming your work, afterall, ;).


Righto. Have a look in a few minutes and let me know if the change is
okay.
One other point, however. You might want to suggest, for GUI related
questions, that the author take the extra step and avoid using the forms
designer(or any designer, for that matter, except where the designer is in
question). Right now its not too big of a deal, however, with the coming of
partial classes the forms designer is going to be one PITA to copy and paste
in. I worry we'll start to see
public partial class MyClass
{
//...
}
public partial class MyClass
{
private void InitalizeComponent()
{
//...
}
}
or simply two attached files.


Yup. Even now it makes the code less readable, IMO - and the more the
designer is used, the more the temptation is to have more controls
which aren't actually required.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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...
Nov 16 '05 #9
Well, thtas good to hear.

That is another benifit of short but complete programs, if you write it and the error isn't there...there is ap retty good chance the error is elsewhere.

Glad you were able to solve your issue, however.
"Alx Sharp" <av*****@hotmail.com> wrote in message news:OK*************@TK2MSFTNGP12.phx.gbl...
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...
Nov 16 '05 #10
>> Being in the first person is fine, but I think atleast some accreditation
is
in order. Something as simple as a "By Jon Skeet" would suffice nicely.
I don't want to appear to be claiming your work, afterall, ;).


Righto. Have a look in a few minutes and let me know if the change is
okay.


Looks good. The additions about designers is effective as well.
Nov 16 '05 #11
It's good to know that we can always find someone like you that helps someone like me...

Thank you so much...
Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tolisss | last post by:
Hi to all i have some validation code inside ListChanged event and i have that collection bound to a grid. If i change a value on the grid the event fires ok. But if i change a value of the...
0
by: Bijoy | last post by:
After Binding a Custom Collection as a Datasource , how is it possible to remove the references in the Property event ListChanged implemented by the IBindingList interface in our Custome...
5
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
12
by: Amy | last post by:
I'm getting a System.StackOverflowException, and I can't see why. Here's the code that's throwing the exception. I don't see anyting that's recursive about it. Any help is appreciated (including...
8
by: Lars-Erik Aabech | last post by:
Hi! I've got an asp.net page that works for all users except one and that one user only gets the error with a certain parameter set to a certain value. (Same value as the others, but for this...
1
by: John | last post by:
When sorting a datagrid by clicking its column header, a ListChanged event is raised by the dataview on that the datagrid is based. However, inside the ListChanged event handler, I loop through the...
10
by: Mae Lim | last post by:
Dear all, I'm new to C# WebServices. I compile the WebService project it return no errors "Build: 1 succeeded, 0 failed, 0 skipped". Basically I have 2 WebMethod, when I try to invoke the...
1
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a...
0
by: Ray Holiday | last post by:
Hi, I'm using the DataView.ListChanged event to catch modifications to my table. But every time I update a row in my table the event fires has many time has I have columns. Since I have 55...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.