Checking the implementation of a property in Reflector as follows:
public object SelectedValue { get { if ((this.SelectedIndex != -1) && (this.dataManager != null)) { object item = this.dataManager[this.SelectedIndex]; return this.FilterItemOnProperty(item, this.valueMember.BindingField); } return null; } set { if (this.dataManager != null) { string bindingField = this.valueMember.BindingField; if (string.IsNullOrEmpty(bindingField)) { throw new InvalidOperationException(SR.GetString("ListControlEmptyValueMemberInSettingSelectedValue")); } PropertyDescriptor property = this.dataManager.GetItemProperties().Find(bindingField, true); int num = this.dataManager.Find(property, value, true); this.SelectedIndex = num; } } }
So this seems to depend on the fact that this.dataManager not null.
If this.dataManager not null, the setter will call Find() with key set to the value you set SelectedValue to:
internal int Find(PropertyDescriptor property, object key, bool keepIndex) { if (key == null) { throw new ArgumentNullException("key"); } if (((property != null) && (this.list is IBindingList)) && ((IBindingList) this.list).SupportsSearching) { return ((IBindingList) this.list).Find(property, key); } if (property != null) { for (int i = 0; i < this.list.Count; i++) { object obj2 = property.GetValue(this.list[i]); if (key.Equals(obj2)) { return i; } } } return -1; }
What will throw an exception if key is null.
I assume that the dataManager set only to a non-zero value when the ComboBox is inserted into the container (e.g. Form), so it will not explode when it is not in the container.
(Actually, dataManager will be non-null if you set the Control.DataSource property to non-zero.)
However, this does not seem to be correct because you reported a NullReferenceException , and this throws an ArgumentNullException explicitly.
[EDIT] It really was an ArgumentNullExeption ; OP is updated accordingly.
Matthew watson
source share