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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Convert "Yes" To Boolean

Convert "Yes" To Boolean

Scheduled Pinned Locked Moved C#
12 Posts 5 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
    StyleGuide
    wrote on last edited by
    #1

    Hi All, Is there a way to Convert a string "Yes" to Boolean - or does this apply only to True/False, 1/0. I've had a look at Parse, Convert.ToBoolean, and BooleanConverter, but no joy. Thanks J

    J T P 3 Replies Last reply
    0
    • S StyleGuide

      Hi All, Is there a way to Convert a string "Yes" to Boolean - or does this apply only to True/False, 1/0. I've had a look at Parse, Convert.ToBoolean, and BooleanConverter, but no joy. Thanks J

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

      string myString = "Yes"; bool myBool = (myString == "Yes") ? true : false; There is no automagic, built-in way, no.

      1 Reply Last reply
      0
      • S StyleGuide

        Hi All, Is there a way to Convert a string "Yes" to Boolean - or does this apply only to True/False, 1/0. I've had a look at Parse, Convert.ToBoolean, and BooleanConverter, but no joy. Thanks J

        T Offline
        T Offline
        Thomas Stockwell
        wrote on last edited by
        #3

        if(val.ToLower()=="yes") valConvert=True else valConvert=False

        Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

        S P L 3 Replies Last reply
        0
        • T Thomas Stockwell

          if(val.ToLower()=="yes") valConvert=True else valConvert=False

          Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

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

          hmmmm.. OK, thanks Chaps. J

          1 Reply Last reply
          0
          • S StyleGuide

            Hi All, Is there a way to Convert a string "Yes" to Boolean - or does this apply only to True/False, 1/0. I've had a look at Parse, Convert.ToBoolean, and BooleanConverter, but no joy. Thanks J

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Yes is a string, and cannot be converted directly to boolean - as it's specific to the English language. If you were to allow this to be translated, then you'd have to take case and culture into account, e.g. Ja, ja, JA, Oui, OUI, oui.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            S L 3 Replies Last reply
            0
            • T Thomas Stockwell

              if(val.ToLower()=="yes") valConvert=True else valConvert=False

              Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Rather than using val.ToLower(), why not use string.Compare with the appropriate case sensitivity test in there?

              if (string.Compare(val, "yes", true) == 0)
              

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              • P Pete OHanlon

                Yes is a string, and cannot be converted directly to boolean - as it's specific to the English language. If you were to allow this to be translated, then you'd have to take case and culture into account, e.g. Ja, ja, JA, Oui, OUI, oui.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

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

                Thanks Pete

                1 Reply Last reply
                0
                • P Pete OHanlon

                  Yes is a string, and cannot be converted directly to boolean - as it's specific to the English language. If you were to allow this to be translated, then you'd have to take case and culture into account, e.g. Ja, ja, JA, Oui, OUI, oui.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  S Offline
                  S Offline
                  StyleGuide
                  wrote on last edited by
                  #8

                  One more thing.. bool boo = Convert.ToBoolean("True"); boo returns as True. If my Culture was "fr-FR", and : bool boo = Convert.ToBoolean("Vrai"); would boo return as True/Vrai? Thanks J

                  J 1 Reply Last reply
                  0
                  • S StyleGuide

                    One more thing.. bool boo = Convert.ToBoolean("True"); boo returns as True. If my Culture was "fr-FR", and : bool boo = Convert.ToBoolean("Vrai"); would boo return as True/Vrai? Thanks J

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

                    No, if you use reflector to look at Convert.ToBoolean you will see that it simply invokes Boolean.TryParse which, decompiled, looks like:

                    public static bool TryParse(string value, out bool result)
                    {
                    result = false;
                    if (value != null)
                    {
                    if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
                    {
                    result = true;
                    return true;
                    }
                    if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
                    {
                    result = false;
                    return true;
                    }
                    if (m_trimmableChars == null)
                    {
                    char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
                    Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
                    destinationArray[destinationArray.Length - 1] = '\0';
                    m_trimmableChars = destinationArray;
                    }
                    value = value.Trim(m_trimmableChars);
                    if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
                    {
                    result = true;
                    return true;
                    }
                    if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
                    {
                    result = false;
                    return true;
                    }
                    }
                    return false;
                    }

                    S 1 Reply Last reply
                    0
                    • J J4amieC

                      No, if you use reflector to look at Convert.ToBoolean you will see that it simply invokes Boolean.TryParse which, decompiled, looks like:

                      public static bool TryParse(string value, out bool result)
                      {
                      result = false;
                      if (value != null)
                      {
                      if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
                      {
                      result = true;
                      return true;
                      }
                      if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
                      {
                      result = false;
                      return true;
                      }
                      if (m_trimmableChars == null)
                      {
                      char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
                      Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
                      destinationArray[destinationArray.Length - 1] = '\0';
                      m_trimmableChars = destinationArray;
                      }
                      value = value.Trim(m_trimmableChars);
                      if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
                      {
                      result = true;
                      return true;
                      }
                      if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
                      {
                      result = false;
                      return true;
                      }
                      }
                      return false;
                      }

                      S Offline
                      S Offline
                      StyleGuide
                      wrote on last edited by
                      #10

                      Ok Thanks

                      1 Reply Last reply
                      0
                      • P Pete OHanlon

                        Yes is a string, and cannot be converted directly to boolean - as it's specific to the English language. If you were to allow this to be translated, then you'd have to take case and culture into account, e.g. Ja, ja, JA, Oui, OUI, oui.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

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

                        jawel, jawohl, ... :-D

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Happy 2008!


                        1 Reply Last reply
                        0
                        • T Thomas Stockwell

                          if(val.ToLower()=="yes") valConvert=True else valConvert=False

                          Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

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

                          no if, no ternary operator: valConvert=val.ToLower()=="yes"; :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          Happy 2008!


                          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