c# - using a method invoker in an if statement -
what trying check whether item in list box selected.
the method being run on separate thread need use method invoker believe.
string list = ""; lbxlist.invoke(new methodinvoker(delegate { list = lbxlist.selecteditem.tostring(); })); if (list != null) { //do }
this code blow if selected item null because string list wont hold it, need way combine top 2 lines if statement checking null.
thanks
this should do:
string list = ""; lbxlist.invoke(new methodinvoker(delegate { if (lbxlist.selecteditem != null) list = lbxlist.selecteditem.tostring(); })); //do
just place if-statement inside anonymous method.
note .tostring
highly unlikely ever return null
anything, documentation of object.tostring
states overriding types should implement method return meaningful value. since know .selecteditem
not null, checking null not necessary. can leave in if want, if you're afraid .tostring
should return null, instead change code this:
string list = ""; lbxlist.invoke(new methodinvoker(delegate { if (lbxlist.selecteditem != null) list = lbxlist.selecteditem.tostring() ?? string.empty; })); //do
Comments
Post a Comment