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. Convert string to boolean

Convert string to boolean

Scheduled Pinned Locked Moved C#
help
7 Posts 4 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.
  • T Offline
    T Offline
    teejayem
    wrote on last edited by
    #1

    Hi i was wondering if someone could help me with converting a string to a boolean so i can do an if statement. what i'm wanting to do is like:

    if (textbox4.text = "true")
    {
    }

    but it gives me an error :-\ thanks for you help in advance! Don't be overcome by evil, but overcome evil with good

    S G D 3 Replies Last reply
    0
    • T teejayem

      Hi i was wondering if someone could help me with converting a string to a boolean so i can do an if statement. what i'm wanting to do is like:

      if (textbox4.text = "true")
      {
      }

      but it gives me an error :-\ thanks for you help in advance! Don't be overcome by evil, but overcome evil with good

      S Offline
      S Offline
      Sean89
      wrote on last edited by
      #2

      You need to have two = signs if you are comparing:

      if(textBox4.Text == "true")
      {
      // do stuff...
      }
      

      You can use bool.Parse() if you need to convert a string to a boolean:

      bool myBool = bool.Parse("true");
      

      Sean :)

      T 1 Reply Last reply
      0
      • T teejayem

        Hi i was wondering if someone could help me with converting a string to a boolean so i can do an if statement. what i'm wanting to do is like:

        if (textbox4.text = "true")
        {
        }

        but it gives me an error :-\ thanks for you help in advance! Don't be overcome by evil, but overcome evil with good

        G Offline
        G Offline
        Graham Nimbley
        wrote on last edited by
        #3

        Single = is assignment Double == is equality

        if (textbox4.text == "true")

        Boolean.Parse() to convert string to bool if needed Sean just beat me to it by seconds! :laugh: -- modified at 21:26 Thursday 27th April, 2006

        S 1 Reply Last reply
        0
        • G Graham Nimbley

          Single = is assignment Double == is equality

          if (textbox4.text == "true")

          Boolean.Parse() to convert string to bool if needed Sean just beat me to it by seconds! :laugh: -- modified at 21:26 Thursday 27th April, 2006

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

          :)


          Sean :)

          1 Reply Last reply
          0
          • S Sean89

            You need to have two = signs if you are comparing:

            if(textBox4.Text == "true")
            {
            // do stuff...
            }
            

            You can use bool.Parse() if you need to convert a string to a boolean:

            bool myBool = bool.Parse("true");
            

            Sean :)

            T Offline
            T Offline
            teejayem
            wrote on last edited by
            #5

            Thanks alot guys to the both of you. [way off topic] Sean i tried to find a way to send you a PM or Email to thank you personally for helping me yesterday. The way you showed me with the listview worked. I just had a problem with the button! But when you was talking about the "Item properties" i was thinking as i was reading that "how in the heck does he remember all theese properties and such". Then it just dawned on me! listview1.items is the exact same thing as going to the properties manager for that control and changing it their. Your just doing the exact same thing but w/o the point n click!. It opened my eyes up to what was actually going on in the code! I was so happy after finding that out i drove home as fast as i could to try to send you a pm telling you thanks but there wasn't a way to do so. but anyways man thanks alot for the help i really appreciate it! Don't be overcome by evil, but overcome evil with good

            S 1 Reply Last reply
            0
            • T teejayem

              Thanks alot guys to the both of you. [way off topic] Sean i tried to find a way to send you a PM or Email to thank you personally for helping me yesterday. The way you showed me with the listview worked. I just had a problem with the button! But when you was talking about the "Item properties" i was thinking as i was reading that "how in the heck does he remember all theese properties and such". Then it just dawned on me! listview1.items is the exact same thing as going to the properties manager for that control and changing it their. Your just doing the exact same thing but w/o the point n click!. It opened my eyes up to what was actually going on in the code! I was so happy after finding that out i drove home as fast as i could to try to send you a pm telling you thanks but there wasn't a way to do so. but anyways man thanks alot for the help i really appreciate it! Don't be overcome by evil, but overcome evil with good

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

              Good to see you got it working! Glad to help:laugh:


              Sean :)

              1 Reply Last reply
              0
              • T teejayem

                Hi i was wondering if someone could help me with converting a string to a boolean so i can do an if statement. what i'm wanting to do is like:

                if (textbox4.text = "true")
                {
                }

                but it gives me an error :-\ thanks for you help in advance! Don't be overcome by evil, but overcome evil with good

                D Offline
                D Offline
                DougW48
                wrote on last edited by
                #7

                A good coding practice is to set your constant on the left side of your operator, so the compiler catches mistakes. For example: if ( "true" == textbox4.text ) { } will assure that you never are doing an assignment if you forget an = when you are intending to do a comparison. It's a good habit to get into.

                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