Exception
-
I have a list box with some items. my code snippet: foreach(string item in listbox1.items) { // some code } it gaives an exception: Unable to cast object of type 'System.Drawing.Point' to type 'System.String'. which is in the first line i.e string item help me with an alternative way
listbox1.items returns a ListItemCollection (you must use System.Collections), it doesn't return a string array. foreach(ListItem item in listbox1.items) { string sText = item.Text; string sValue = item.Value; // some code } Regards.
Just call me Valy... :)
-
I have a list box with some items. my code snippet: foreach(string item in listbox1.items) { // some code } it gaives an exception: Unable to cast object of type 'System.Drawing.Point' to type 'System.String'. which is in the first line i.e string item help me with an alternative way
Hi, a ListBox item can have any type you choose, all the items in a ListBox dont even have to have the same type. Foreach insists that each item in the collection has the indicated type, otherwise an Exception will be trown. If the types differ and you want to process only one of the types, say you only want to process the strings, then do the following:
foreach (object item in collection) {
string str=item as string;
if (str!=null) {
.. handle the str
}
}:)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
listbox1.items returns a ListItemCollection (you must use System.Collections), it doesn't return a string array. foreach(ListItem item in listbox1.items) { string sText = item.Text; string sValue = item.Value; // some code } Regards.
Just call me Valy... :)
-
Thanks for the suggestion... But when I use foreach(ListItem item in listbox1.items) { } It throws an exception saying ListItem is not a type or namespace are u missing a assembly reference or an using directive
sangramkp wrote:
are u missing a assembly reference or an using directive
Well, Are you?
ListItem
is in theSystem.Web.UI.WebControls
namespace. It is probably the 'using' you are missing rather than the assembly reference. -
Hi, a ListBox item can have any type you choose, all the items in a ListBox dont even have to have the same type. Foreach insists that each item in the collection has the indicated type, otherwise an Exception will be trown. If the types differ and you want to process only one of the types, say you only want to process the strings, then do the following:
foreach (object item in collection) {
string str=item as string;
if (str!=null) {
.. handle the str
}
}:)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
sangramkp wrote:
are u missing a assembly reference or an using directive
Well, Are you?
ListItem
is in theSystem.Web.UI.WebControls
namespace. It is probably the 'using' you are missing rather than the assembly reference. -
Hi pattyn While i am trying to use foreach(ListItem item in listbox1.Items) { } It shows an error: Error 1 The type or namespace name 'ListItem' could not be found (are you missing a using directive or an assembly reference?)
So ? I never told you to use the ListItem class, did I ?
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
sangramkp wrote:
are u missing a assembly reference or an using directive
Well, Are you?
ListItem
is in theSystem.Web.UI.WebControls
namespace. It is probably the 'using' you are missing rather than the assembly reference. -
So ? I never told you to use the ListItem class, did I ?
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Thanks. I devised it to encourage people to search CodeProject before asking simple questions for which the answers are readily available... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Thanks. I devised it to encourage people to search CodeProject before asking simple questions for which the answers are readily available... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Except it hasn't worked in this case ;P
only two letters away from being an asset
-
Except it hasn't worked in this case ;P
only two letters away from being an asset
Yeah, what would we do without simple questions ? We would have to investigate the hard ones !? :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }