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. Other Discussions
  3. The Weird and The Wonderful
  4. smart way to detect if System.Boolean true or false

smart way to detect if System.Boolean true or false

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
19 Posts 11 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.
  • V Offline
    V Offline
    Vladimir Svyatski
    wrote on last edited by
    #1

    I've just found an "excellent" way to detect if boolean value is true or not in C#. Just great! Important, "value" variable is System.Boolean.

                string valueStr = value.ToString();
                if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                {
                    PDFIncludeWatermark\_ = "true";
                }
                else
                {
                    PDFIncludeWatermark\_ = "false";
                }
    
    M OriginalGriffO B B 4 Replies Last reply
    0
    • V Vladimir Svyatski

      I've just found an "excellent" way to detect if boolean value is true or not in C#. Just great! Important, "value" variable is System.Boolean.

                  string valueStr = value.ToString();
                  if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                  {
                      PDFIncludeWatermark\_ = "true";
                  }
                  else
                  {
                      PDFIncludeWatermark\_ = "false";
                  }
      
      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      :laugh: haha, what a joke! You could make this so much simpler by just having...

      PDFIncludeWatermark_ = value.ToString().ToLowerInvarient().Contains("true") ? "true" : "false";

      I may or may not be responsible for my own actions

      L P V R 4 Replies Last reply
      0
      • M musefan

        :laugh: haha, what a joke! You could make this so much simpler by just having...

        PDFIncludeWatermark_ = value.ToString().ToLowerInvarient().Contains("true") ? "true" : "false";

        I may or may not be responsible for my own actions

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

        Contains!=Equals :~

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        M A 2 Replies Last reply
        0
        • M musefan

          :laugh: haha, what a joke! You could make this so much simpler by just having...

          PDFIncludeWatermark_ = value.ToString().ToLowerInvarient().Contains("true") ? "true" : "false";

          I may or may not be responsible for my own actions

          P Offline
          P Offline
          Paladin2000
          wrote on last edited by
          #4

          PDFIncludeWatermark_ = value.ToString().ToLower();

          or

          PDFIncludeWatermark_ = value ? "true" : "false";

          M 1 Reply Last reply
          0
          • L Luc Pattyn

            Contains!=Equals :~

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            Future proof... in case Microsoft want to emphasize the System.Boolean.ToString() value and return "true!"; always got to be on your toes ;)

            I may or may not be responsible for my own actions

            1 Reply Last reply
            0
            • P Paladin2000

              PDFIncludeWatermark_ = value.ToString().ToLower();

              or

              PDFIncludeWatermark_ = value ? "true" : "false";

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              now your just being silly

              I may or may not be responsible for my own actions

              1 Reply Last reply
              0
              • M musefan

                :laugh: haha, what a joke! You could make this so much simpler by just having...

                PDFIncludeWatermark_ = value.ToString().ToLowerInvarient().Contains("true") ? "true" : "false";

                I may or may not be responsible for my own actions

                V Offline
                V Offline
                Vladimir Svyatski
                wrote on last edited by
                #7

                I like your way of thinking... :laugh: Good optimization, you know.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Contains!=Equals :~

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #8

                  Surely you mean: !Equals.Contains(Contains)

                  [WikiLeaks Cablegate Cables]

                  1 Reply Last reply
                  0
                  • V Vladimir Svyatski

                    I've just found an "excellent" way to detect if boolean value is true or not in C#. Just great! Important, "value" variable is System.Boolean.

                                string valueStr = value.ToString();
                                if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    PDFIncludeWatermark\_ = "true";
                                }
                                else
                                {
                                    PDFIncludeWatermark\_ = "false";
                                }
                    
                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    I'm not sure I trust computers that much. Can I suggest an improvement?

                                string valueStr = value.ToString();
                                if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    PDFIncludeWatermark\_ = "true";
                                }
                                else if (valueStr.Equals("false", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    PDFIncludeWatermark\_ = "false";
                                }
                                else
                                {
                                    PDFIncludeWatermark\_ = "maybe";
                                }
                    

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    V T 2 Replies Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      I'm not sure I trust computers that much. Can I suggest an improvement?

                                  string valueStr = value.ToString();
                                  if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                  {
                                      PDFIncludeWatermark\_ = "true";
                                  }
                                  else if (valueStr.Equals("false", StringComparison.CurrentCultureIgnoreCase))
                                  {
                                      PDFIncludeWatermark\_ = "false";
                                  }
                                  else
                                  {
                                      PDFIncludeWatermark\_ = "maybe";
                                  }
                      

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                      V Offline
                      V Offline
                      Vladimir Svyatski
                      wrote on last edited by
                      #10

                      You're right, never, never rely on compiler. Damn machines :) If you need a job done, do it yourself.

                      OriginalGriffO 1 Reply Last reply
                      0
                      • V Vladimir Svyatski

                        You're right, never, never rely on compiler. Damn machines :) If you need a job done, do it yourself.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        Oh, I trust the compiler. It's reality I don't trust.:~

                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • V Vladimir Svyatski

                          I've just found an "excellent" way to detect if boolean value is true or not in C#. Just great! Important, "value" variable is System.Boolean.

                                      string valueStr = value.ToString();
                                      if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                      {
                                          PDFIncludeWatermark\_ = "true";
                                      }
                                      else
                                      {
                                          PDFIncludeWatermark\_ = "false";
                                      }
                          
                          B Offline
                          B Offline
                          Bernhard Hiller
                          wrote on last edited by
                          #12

                          Happy debugging! Internationalization will make you happy. I experienced that a long time ago. See also my message in the Hall of Shame: http://www.codeproject.com/feature/HallOfSHame.aspx?msg=3432553#xx3432553xx[^]

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            I'm not sure I trust computers that much. Can I suggest an improvement?

                                        string valueStr = value.ToString();
                                        if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            PDFIncludeWatermark\_ = "true";
                                        }
                                        else if (valueStr.Equals("false", StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            PDFIncludeWatermark\_ = "false";
                                        }
                                        else
                                        {
                                            PDFIncludeWatermark\_ = "maybe";
                                        }
                            

                            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                            T Offline
                            T Offline
                            Tallmaris
                            wrote on last edited by
                            #13

                            The else section should return "filenotfound" I believe! ;P

                            else
                            {
                            PDFIncludeWatermark_ = "filenotfound";
                            }

                            FTFY :-D

                            OriginalGriffO R 2 Replies Last reply
                            0
                            • T Tallmaris

                              The else section should return "filenotfound" I believe! ;P

                              else
                              {
                              PDFIncludeWatermark_ = "filenotfound";
                              }

                              FTFY :-D

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #14

                              I disagree: the concepts of truth and falsehood do not have anything to do with file systems! However, in the real world I doubt the existence of "absolute truth" and "absolute falsehood" so a "maybe" response seems reasonable... :laugh:

                              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              T 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                I disagree: the concepts of truth and falsehood do not have anything to do with file systems! However, in the real world I doubt the existence of "absolute truth" and "absolute falsehood" so a "maybe" response seems reasonable... :laugh:

                                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                T Offline
                                T Offline
                                Tallmaris
                                wrote on last edited by
                                #15

                                eheh, you are right. Anyway, I was just quoting one of the most famous WTF: http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx[^] :D

                                OriginalGriffO 1 Reply Last reply
                                0
                                • T Tallmaris

                                  eheh, you are right. Anyway, I was just quoting one of the most famous WTF: http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx[^] :D

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #16

                                  Not seen that before - thanks!

                                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                  1 Reply Last reply
                                  0
                                  • T Tallmaris

                                    The else section should return "filenotfound" I believe! ;P

                                    else
                                    {
                                    PDFIncludeWatermark_ = "filenotfound";
                                    }

                                    FTFY :-D

                                    R Offline
                                    R Offline
                                    Rick Shaub
                                    wrote on last edited by
                                    #17

                                    Dang! Beat me to it. Marking it as "news" is pretty hilarious.

                                    1 Reply Last reply
                                    0
                                    • V Vladimir Svyatski

                                      I've just found an "excellent" way to detect if boolean value is true or not in C#. Just great! Important, "value" variable is System.Boolean.

                                                  string valueStr = value.ToString();
                                                  if (valueStr.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                                                  {
                                                      PDFIncludeWatermark\_ = "true";
                                                  }
                                                  else
                                                  {
                                                      PDFIncludeWatermark\_ = "false";
                                                  }
                                      
                                      B Offline
                                      B Offline
                                      Baruch23
                                      wrote on last edited by
                                      #18

                                      Dear VUnreal, Please find below a simplified code segment that may help you in achieving your code in one line using a helper class. please feel free to add additional checks and safeguards, and rewrite the helper as an extension to the string class.

                                      string valueStr = value.ToString();
                                      TruthChecker tc=new TruthChecker();
                                      // now notice how simple: we do not use 'if', just call our helper once:
                                      PDFIncludeWatermark_ = tc.CheckIfTrue();

                                      class TruthChecker
                                      {
                                          private static List<string> \_truths = null; // static - optimaze time as the calculation is only done if needed and even so , only once!
                                          List<string> Truths { get { return \_truths!=null ? \_truths : (\_truths = FindAllTruths() ); } }
                                      
                                          TruthChecker() {
                                          }
                                      
                                          private static List<string> FindAllTruths() {
                                               List<string> allTruths = new List<string>();
                                               char\[\] t = {'t','r','u','e'};
                                               for(int i=0;i<Math.Pow(2,t.Length); i++) {
                                                  StringBuilder sb = new StringBuilder();
                                                  for(int j=0; j<t.Length; j++) {
                                                      int k = (i>>j)&1;
                                                      sb.Append(t\[j\]+('A'-'a')\*k);
                                                  }
                                                  allTruths.Add(sb.ToString());
                                              }
                                              return allTruths;
                                          }
                                      
                                          public string CheckIfTrue(string tru)
                                          {
                                              try {
                                                  var v = 
                                                      from t in Truths
                                                      where t == tru
                                                      select t;
                                      
                                                  if (v.ElementAt(0).ToString().Length == "true".Length) {
                                                      return "true";
                                                  }
                                              } catch (Exception ex) {
                                                  tru = ex.Message;
                                              }
                                              return "false";
                                          }
                                      }
                                      

                                      Please visit IOCCC.org to learn more about useful coding techniques.

                                      1 Reply Last reply
                                      0
                                      • M musefan

                                        :laugh: haha, what a joke! You could make this so much simpler by just having...

                                        PDFIncludeWatermark_ = value.ToString().ToLowerInvarient().Contains("true") ? "true" : "false";

                                        I may or may not be responsible for my own actions

                                        R Offline
                                        R Offline
                                        R Erasmus
                                        wrote on last edited by
                                        #19

                                        depends what type of programming style you're into. Some might find the mentioned method easier/simpler to read/understand.

                                        "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                                        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