c# - Why does setting ComboBox.SelectedValue to null cause a ArgumentNullException? -
why setting selectedvalue of combobox null cause argumentnullexception?
the exception occurs if combobox part of form. can set selectedvalue kinds of values or types don't make sense, can't set null.
it's not selectedvalue can not null. in fact, value is null @ time i'm trying set null.
in real code, doesn't happen in constructor, , i'm not excplicitly setting null. code using variable happens null. can fix checking of variable isn't null before trying set selectedvalue. don't understand why can't set null value.
code edit: datasource contains item valuemembers value null
using system.collections.generic; using system.windows.forms; public class form1 : form { public form1() { var combobox1 = new combobox(); controls.add(combobox1); combobox1.valuemember = "key"; combobox1.displaymember = "value"; combobox1.datasource = new list<record> { new record {key = "1", value = "one"}, new record {key = null, value = "null"} }; combobox1.selecteditem = null; // no problem combobox1.selectedvalue = ""; // no problem combobox1.selectedvalue = new object(); // no problem combobox1.selectedvalue = null; // argumentnullexception!! } } public class record { public string key { get; set; } public string value { get; set; } }
inspecting implementation of property in reflector looks this:
public object selectedvalue { { 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 seems hinge on this.datamanager not being null.
if this.datamanager isn't null, setter call find() key set value you're setting selectedvalue to:
internal int find(propertydescriptor property, object key, bool keepindex) { if (key == null) { throw new argumentnullexception("key"); } if (((property != null) && (this.list ibindinglist)) && ((ibindinglist) this.list).supportssearching) { return ((ibindinglist) this.list).find(property, key); } if (property != null) { (int = 0; < this.list.count; i++) { object obj2 = property.getvalue(this.list[i]); if (key.equals(obj2)) { return i; } } } return -1; } which throw exception if key null.
i'm guessing datamanager set non-null when combobox inserted container (e.g. form) why doesn't blow when it's not in container.
(in fact, datamanager set non-null when set control.datasource property non-null.)
however, doesn't seem quite right, because reported nullreferenceexception , throws argumentnullexception.
[edit] indeed argumentnullexeption; op has been updated accordingly.
Comments
Post a Comment