Check if object still exists

Posted on 2005-05-09
Medium Priority
Last Modified: 2010-04-16
I've got a main dialog, which itself creates new dialogs. Every time I create a new dialog, I do this:

NoteForm note = new NoteForm();

That means that I save a reference to the dialog in an ArrayList called noteObjects. The dialogs can be minimized and hidden. I added a button in my main dialog that shows all dialogs. So what I do is iterating through the ArrayList and call:

for(i=0;i<noteObjects.Count;i++) {

If a dialog has been closed, there are invalid entries in the array which cause a System.ObjectDisposedException

How can I remove array elements that have become invalid?
if ((NoteForm)noteObjects[i] == null) ... doesn't work.

Thanks for any suggestion,
Question by:marb
LVL 48

Accepted Solution

AlexFM earned 500 total points
ID: 13959051
Subscribe to Closed event for each created form. In this event handler remove sender from noteObjects list.

Author Comment

ID: 13959206
I've added this line in my main dialog:

note.Closing += new System.ComponentModel.CancelEventHandler(this.NoteFormClosing);

and added a event handler:

void NoteFormClosing(object sender, System.ComponentModel.CancelEventArgs e)

It works fine. Thank you AlexFM!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question