I'm curious about something.

If I have a listbox that is databound and a filter is applied to that listbox based on the value in a combobox, what would be the best way to handle the null reference exception thrown when the form loads or the filter changes and everything is filtered out. Right now I just have a "IsLoading" and "IsClosing" bool value that I change in those two events and just have a simple if statement to basically ignore the exception if either of those values is true.

But I'm not sure if this is the best way to handle this. I keep thinking that if a method is throwing an exception and you just "filter" the exceptions like I am doing and ignore some, it could be a problem later.

Any suggestions/thoughts?

Can you test the contents first to make sure you're not going to get an Exception?
If you're using Linq, can you set your "where" clause to only get data that exists in the master list?

I'm not using Linq, I can't stand it, but I could check for the data first, I'm just worried about speed with something like that. I'll check that out! Thanks for the suggestion!

If it never changes or it is small, it's not a problem.
Handling an exception in a loop might take more time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.