@anotherusername said:
@mikeTheLiar said:
In the spirit of today's front page article, here's a WTF that I found a while ago that I've just been sitting on.
This is from a class that inherits from <font face="Lucida Console" size="2">System.Web.UI.WebControls.DropDownList</font> and the end result is a fancy drop down with all sorts of bells and whistles and special formatting - which, to be fair, is a non-trivial task. But then there's this:
public class dropdownlistadapter : System.Web.UI.WebControls.DropDownList
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
for (int i = 0; i < items.Count; i++)
{
bool flag = false;
//snip some irrelvant code that does nothing with flag
if (item.Selected)
{
if (flag)
{
this.VerifyMultiSelect();
}
flag = true;
}
<span style="color:#800080;">}</span>
<span style="color:#800080;">}</span>
}
Do you see it? No, not the fact that flag is always false. Look closer. Here, I'll let Intellisense help:
And a link to the docs for good measure.
Yes, this code could be re-written as if(false) { throw new HttpException(); }
I don't have the faintest idea of what this was actually trying to accomplish.
It's very simple. It checks every item in the dropdown. If it finds a selected item, it sets the flag. If it finds another selected item (when the flag is already set), it checks to ensure that the System.Web.UI.WebControls.DropDownList allows multiple selections.
I.e.:
Does the list have more than 1 item selected?
If so, does it allow multiple selection?
If not, an HttpException will be thrown.
It doesn't support multiple selections, so that code can't possibly find more than one selected item, so VerifyMultiSelect() will never run and it will never throw that exception, but that's kind of beside the point.
tl;dr: someone thought that it was a good idea to check to make sure that a dropdown that doesn't allow multiple selections should never have more than 1 item selected
That's a pretty reasonable explanation. Unfortunately flag is scoped within the for block, so it's discarded at the end of every iteration. There is no case where VerifyMultiSelect() will ever be called. If you move the flag declaration out of the for loop, then it would work as you describe, and would still be entirely pointless.