Click here to Skip to main content
15,881,709 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: smart way to detect if System.Boolean true or false Pin
OriginalGriff12-Mar-11 4:59
mveOriginalGriff12-Mar-11 4:59 
NewsRe: smart way to detect if System.Boolean true or false Pin
Tallmaris15-Mar-11 0:48
Tallmaris15-Mar-11 0:48 
GeneralRe: smart way to detect if System.Boolean true or false Pin
OriginalGriff15-Mar-11 0:52
mveOriginalGriff15-Mar-11 0:52 
GeneralRe: smart way to detect if System.Boolean true or false Pin
Tallmaris15-Mar-11 1:23
Tallmaris15-Mar-11 1:23 
GeneralRe: smart way to detect if System.Boolean true or false Pin
OriginalGriff15-Mar-11 1:27
mveOriginalGriff15-Mar-11 1:27 
GeneralRe: smart way to detect if System.Boolean true or false Pin
Rick Shaub15-Mar-11 7:51
Rick Shaub15-Mar-11 7:51 
GeneralRe: smart way to detect if System.Boolean true or false Pin
Bernhard Hiller14-Mar-11 3:50
Bernhard Hiller14-Mar-11 3:50 
GeneralRe: smart way to detect if System.Boolean true or false Pin
Baruch2322-Mar-11 14:39
professionalBaruch2322-Mar-11 14:39 
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.

C#
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.
GeneralException handling PinPopular
Thallian3-Mar-11 19:57
Thallian3-Mar-11 19:57 
GeneralRe: Exception handling Pin
musefan3-Mar-11 22:19
musefan3-Mar-11 22:19 
GeneralRe: Exception handling Pin
Sander Rossel3-Mar-11 23:29
professionalSander Rossel3-Mar-11 23:29 
GeneralRe: Exception handling Pin
musefan4-Mar-11 0:30
musefan4-Mar-11 0:30 
JokeRe: Exception handling Pin
_Erik_4-Mar-11 1:50
_Erik_4-Mar-11 1:50 
GeneralRe: Exception handling Pin
GenJerDan4-Mar-11 4:10
GenJerDan4-Mar-11 4:10 
JokeRe: Exception handling Pin
Sander Rossel4-Mar-11 8:41
professionalSander Rossel4-Mar-11 8:41 
GeneralRe: Exception handling Pin
Bernhard Hiller4-Mar-11 4:37
Bernhard Hiller4-Mar-11 4:37 
GeneralRe: Exception handling Pin
Dalek Dave4-Mar-11 13:23
professionalDalek Dave4-Mar-11 13:23 
GeneralRe: Exception handling Pin
Jörgen Andersson6-Mar-11 20:09
professionalJörgen Andersson6-Mar-11 20:09 
GeneralRe: Exception handling Pin
Ramalinga Koushik11-Mar-11 0:33
Ramalinga Koushik11-Mar-11 0:33 
GeneralRe: Exception handling Pin
Ramalinga Koushik11-Mar-11 0:32
Ramalinga Koushik11-Mar-11 0:32 
GeneralRe: Exception handling Pin
RobCroll4-Mar-11 15:52
RobCroll4-Mar-11 15:52 
GeneralRe: Exception handling Pin
Sander Rossel6-Mar-11 12:31
professionalSander Rossel6-Mar-11 12:31 
GeneralRe: Exception handling Pin
RobCroll6-Mar-11 15:29
RobCroll6-Mar-11 15:29 
GeneralRe: Exception handling Pin
Sander Rossel6-Mar-11 20:16
professionalSander Rossel6-Mar-11 20:16 
GeneralRe: Exception handling Pin
Quirkafleeg15-Mar-11 1:23
Quirkafleeg15-Mar-11 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.