On C# I am getting the CS1061 Error stating ,Object does not have a definition for "Items" and no Extension Method for "Items" -
here coding. have researched through out website. many people seem have same issue, not related mine. writing program access pictures laptop, error not letting me access picture directory. highly appreciated!!!
currentdir = fb.selectedpath; // selected folder user; textboxd.text = currentdir; var dirinfo = new directoryinfo(currentdir); var files = dirinfo.getfiles().where(c=>c.extension.equals(".jpg") || c.extension.equals(".jpeg") || c.extension.equals(".bmp") || c.extension.equals(".png")); foreach (var image in files) { object listboximages = null; //add images/files list box listboximages.items.add(image.name); }
you have 2 problems. first, immediate cause of compilation error listboximages defined object. has methods , properties of object - not have items property.
secondly, assigning null listboximages if had items property you'd nullreferenceexception
i'm assuming want initialize listbox instead. when make sure define , initialize outside of foreach loop because otherwise contain last item
in addition can refactor linq bit use hashset of valid extensions:
var validextensions = new hashset<string>(new[] { ".jpg", "jpeg", ".bmp", ".png" }); var files = dirinfo.getfiles().where(c => validextensions.contains(c.extension)); hashset contains performed in o(1) changing multiple || statements using collection doesn't harm performance , more extensible , readable
Comments
Post a Comment