Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exception

Exception

Scheduled Pinned Locked Moved C#
graphicshelp
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sangramkp
    wrote on last edited by
    #1

    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

    V L 2 Replies Last reply
    0
    • S sangramkp

      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

      V Offline
      V Offline
      valerian precop
      wrote on last edited by
      #2

      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... :)

      S 1 Reply Last reply
      0
      • S sangramkp

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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] }


        S 1 Reply Last reply
        0
        • V valerian precop

          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... :)

          S Offline
          S Offline
          sangramkp
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • S sangramkp

            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

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            sangramkp wrote:

            are u missing a assembly reference or an using directive

            Well, Are you? ListItem is in the System.Web.UI.WebControls namespace. It is probably the 'using' you are missing rather than the assembly reference.

            --- How to get answers to your questions[^]

            S 2 Replies Last reply
            0
            • L Luc Pattyn

              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] }


              S Offline
              S Offline
              sangramkp
              wrote on last edited by
              #6

              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?)

              L 1 Reply Last reply
              0
              • J J4amieC

                sangramkp wrote:

                are u missing a assembly reference or an using directive

                Well, Are you? ListItem is in the System.Web.UI.WebControls namespace. It is probably the 'using' you are missing rather than the assembly reference.

                --- How to get answers to your questions[^]

                S Offline
                S Offline
                sangramkp
                wrote on last edited by
                #7

                But I am working on windows Application...

                1 Reply Last reply
                0
                • S sangramkp

                  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?)

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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] }


                  R 1 Reply Last reply
                  0
                  • J J4amieC

                    sangramkp wrote:

                    are u missing a assembly reference or an using directive

                    Well, Are you? ListItem is in the System.Web.UI.WebControls namespace. It is probably the 'using' you are missing rather than the assembly reference.

                    --- How to get answers to your questions[^]

                    S Offline
                    S Offline
                    sangramkp
                    wrote on last edited by
                    #9

                    But i am working on a windows forms application

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      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] }


                      R Offline
                      R Offline
                      ruanr
                      wrote on last edited by
                      #10

                      I really like your sig XD

                      L 1 Reply Last reply
                      0
                      • R ruanr

                        I really like your sig XD

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        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] }


                        N 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          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] }


                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #12

                          Except it hasn't worked in this case ;P


                          only two letters away from being an asset

                          L 1 Reply Last reply
                          0
                          • N Not Active

                            Except it hasn't worked in this case ;P


                            only two letters away from being an asset

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            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] }


                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups