I'm reviewing some code and I came across this:

// Select the last parameter in the list
if (parametersListBox.SelectedIndex > -1)
    parametersListBox.SetSelected(parametersListBox.SelectedIndex, true);

ParametersListBox being a C# ListBox. Despite the fact that the comment is wrong, does this actually do something or is this just useless code?


closed as off-topic by 422_unprocessable_entity, Malachi, Brythan, rolfl Oct 31 '14 at 15:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


It does appear to be useless but it might be firing some bindings that aren't setup correctly in the first place.

Check: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.setselected%28v=vs.110%29.aspx

For more on set selected.

Only thing I can suggest is to comment it out and do some testing.


The check is needed, because SetSelected() would throw an ArgumentOutOfRangeException.

See reference source: http://referencesource.microsoft.com/System.Windows.Forms/a.html#fcae9a622a947f54 or read the documentation where it clearly states

Condition for ArgumentOutOfRangeException

The specified index was outside the range of valid values.


Not the answer you're looking for? Browse other questions tagged or ask your own question.