.net - Saving selected listbox(binded) items to an array in c# -
string[] chkitems = new string[4]; string[] str = new string[4]; str[0] = txtid.text; str[1] = txtname.text; str[2] = txtemail.text; itemcount = ltbxinterests.selecteditems.count; (int = 0; <= itemcount; i++) { ltbxinterests.selecteditems.copyto(chkitems, 0); // here returning exception //"object cannot stored in array of type." }
please me how out exception
couple issues here, chkitems defined length 4 exception if try , put more 4 items in. source array selecteditems of type object need cast result.
assuming putting strings listbox use (remember reference system.linq)
string[] str = new string[4]; str[0] = txtid.text; str[1] = txtname.text; str[2] = txtemail.text; string[] chkitems = ltbxinterests.selecteditems.oftype<string>().toarray();
if wanting limit first 4 items, replace last line to
string[] chkitems = ltbxinterests.selecteditems.oftype<string>().take(4).toarray();
also shorten code use array initializer (but wil make str length 3 because have 3 items):
string[] str = new [] { txtid.text, txtname.text, txtemail.text, }
Comments
Post a Comment