Check if object still exists

Posted on 2005-05-09
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

    Subscribe to Closed event for each created form. In this event handler remove sender from noteObjects list.

    Author Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    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.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now