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?


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.

|improve this answer|||||

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.

|improve this answer|||||

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