tags:

views:

274

answers:

5

Sorry about the weird question title, but I don't really know what to call this. It simply makes no sense to me. Here is the code:

public partial class ParameterPanel : FlowLayoutPanel
{
    ...

    public void SetContents(IEnumerable<IParameter> parameters)
    {
        if (parameters == null || !parameters.Any())
            return;

        SuspendLayout();
        Controls.Clear();

        foreach (IParameter parameter in parameters)
        {
            Control control = Factory.Create(parameter);
            Controls.Add(control);
        }

        Console.WriteLine("???");

        ResumeLayout(false);
        PerformLayout();
    }
}

The weird thing is that the code sometimes never gets to the Console.WriteLine I break in the beginning of the method and try to step through it. It goes into the foreach loop, but after the last item, the method just returns?? It never reaches the Console.WriteLine. And I just don't get it... How is this even possible? And the weirdest thing is that it doesn't happen always either. But it happens consistently in the cases it does.

Anyone have a clue what is going on here? I don't even know where to start looking for this bug.

A: 

Add check if control has been created successfully, if not then skip adding.

foreach (IParameter parameter in parameters)
{
    Control control = Factory.Create(parameter);
    if (control!=null) {
        Controls.Add(control);
    }
}
Rafal Ziolkowski
+1  A: 

One oddity: if parameters is null or empty, you're never resuming or performing the layout... I know that's not the situation you're running into, but it's something to fix.

The other possibility is that an exception is being thrown somewhere in the loop.

Jon Skeet
Oooh, that's true. Thanks for letting me know!
Svish
Fixed to code, so this shouldn't be the case now. I hope...
Svish
+4  A: 

If something in the method is throwing an exception the rest of the method will be skipped.

Hit Debug->Exceptions and tick the box for CLR exceptions to make VS break as soon as an exception is thrown.

Simon P Stevens
Yeah, found it using that. But why didn't it break in the first place? All I got was "A first chance exception of type 'System.NullReferenceException' occurred in TheApp.dll", and I don't understand where it got eaten.
Svish
Is something higher up catching the exception. I noticed you're inheriting from the FlowLayoutPanel; is set contents being caught from some overridden method that has a caller in FlowLayoutPanel that handles the exception? Try handling the AppDomain.CurrentDomain.UnhandledException event to see if it's occurring on a background thread - although as this looks like GUI code I'm assuming it's not on a background thread.
Simon P Stevens
A: 

May be an exception occur in the foreach, try to check Debug->Exceptions-> Common language runtime exceptions, to see is there any exception or not

Ahmed Said
A: 

As Jon Skeet says, the line -

if (parameters == null || !parameters.Any()) return;

is returning the function so in that case you'll never get any farther. It's common to enforce this kind of contract at the beginning of a method but you'd usually throw an exception in this case.

Stimul8d
Yeah, but in my case this wasn't the problem. The method did run past that. Anyways, the issue was caused by an exception that was thrown in the constructor of a control.
Svish