Reputation: 157981
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.
Upvotes: 1
Views: 429
Reputation: 11795
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.
Upvotes: 0
Reputation: 7238
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
Upvotes: 0
Reputation: 27499
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.
Upvotes: 4
Reputation: 1499830
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.
Upvotes: 1
Reputation:
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);
}
}
Upvotes: 0