Get Field, no matter where it is



  • Continuing my CodeSODs from my company's library, here's a fun method:

            /// <summary>
            /// Gets a specified field from the query string of the url
            /// </summary>
            /// <param name="p">current Page</param>
            /// <param name="strField">specified field</param>
            /// <returns>the value of the query string</returns>
            static public string getField(Page p, string strField)
            {
                string strValue = string.Empty;
    
                try
                {
                    strValue = p.Request.QueryString.Get(strField);
                    if (null != strValue)
                        return strValue.Trim();
                }
                catch (Exception ex) { }
    
                try
                {
                    strValue = p.Request.Form[strField];
                    if (null != strValue)
                        return strValue.Trim();
                }
                catch (Exception ex) { }
    
                return string.Empty;
            }
    

    Not only is the documentation wrong in that it states that it searches for the value in the query string (it

    does that AND the form), but it's completely over-engineering something simple. I'm going to put aside the

    fact that not knowing if the value you're looking for is in the QueryString or Form is pretty bad, or that

    empty catch blocks are a sign of bad code, and just state that looking in the QueryString isn't that hard:

    string val = Request.QueryString["fieldname"] == null ? "" : Request.QueryString["fieldname"].ToString();
    

    That wasn't that hard, was it?



  • @chubertdev said:

    string val = Request.QueryString["fieldname"] == null : "" ? Request.QueryString["fieldname"].ToString();

    Or

    string val = Request.QueryString[strField] ?? "";
    


  • Whoops, totally got that backwards.



  • Btw, try to specify language highlighting when posting code, it seems to work.

    ``'c#
    // code here
    ``'
    

    Replaced the last ` with '. Stupid markdown.



  • Much better.



  • StackOverflow has a pretty nifty automatic detection mechanism, where it will try to detect your programming language and apply the correct formatting. Maybe @codinghorror can nab it and expose it to the tech-oriented discourse users (like a plugin, perhaps)?



  • It would only make sense.

    "If I can't see it, it's not there."

    -- Users


  • Banned

    Should work. The style is fenced code blocks from GitHub

    Automatic detection is not always reliable (and does happen, by the way), but manually specifying should work. Not sure if the correct string here is c# or csharp though.


  • Banned

    The format is

    ```cs
    some code
    ```

    But auto detection works fine, usually.

    using System;
    
    #pragma warning disable 414, 3021
    
    public class Program
    {
        /// <summary>The entry point to the program.</summary>
        public static int Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            string s = @"This
    ""string""
    spans
    multiple
    lines!";
            return 0;
        }
    }
    
    async Task<int> AccessTheWebAsync()
    {
        // ...
        string urlContents = await getStringTask;
        return urlContents.Length;
    }
    

    see: http://highlightjs.org/static/test.html



  • 'c#' seems to be working as well.

    namespace TDWTF {
      public class Test {
        public static void Main(string[] argv)  {
          var x = 1 + 1;
          var y = "test";
        }
      }
    }
    

    You're right, auto-detection seems to be working on this example. Not sure why it failed to initially mark chubertdev's post.



  • I'd like to see strikethrough, and tables; they may be useful as:

    strikeout with HTML tags sucks≣too much typing
    and manual tables suck too!



  • I'd like to see pandoc as Discourse's markdown converter. It supports all that stuff and doesn't have the weird quirks that the current converter has.


  • Banned

    Unlikely, we need preview parity and Emscripten seems like a rather unlikely adventure we would take.

    That said, I do like marked and at least I have a fighting chance at hacking that code. https://github.com/chjj/marked



  • <script type="text/haskell">
    

    Edit: https://github.com/ghcjs/ghcjs looks nice...



  • @ben_lubar said:

    I'd like to see pandoc…

    @sam said:

    Unlikely, we need preview parity and Emscripten seems like a rather unlikely adventure we would take.

    That said, I do like marked and at least I have a fighting chance at hacking that code. https://github.com/chjj/marked


    @ben_lubar said:
    <script type="text/haskell">;

    https://github.com/ghcjs/ghcjs looks nice...

    Pandoc would be sweet (LaTeX !), but as a step up, marked looks ok…



  • @cartman82 said:

    You're right, auto-detection seems to be working on this example. Not sure why it failed to initially mark chubertdev's post.

    I wonder if the XML threw it off.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.