Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Discussions

Bent Rasmussen exoteric stuck in a loop, for a while
  • Command line argument parser for .Net

    Downloading...

  • Extensia

    Seeing Sven's parameter parsing library made me think of publishing a very simple technique I've been using for a couple of small console applications. Material for another blog post.

     

    Example code first

     

    static void Main(string[] args) { // parse flag - naturally typed as bool var a = args.Argued("fast"); Console.WriteLine("fast("+a+")"); // parse parameter, naturally typed as string var b = args.Argument("price"); Console.WriteLine("price("
     + b + ")"); // parse parameter into typed value, naturally typed as T? var c = args.Argument("price").Eval<decimal>(); Console.WriteLine("price<" + c + ">"); // parse parameter into typed value, with sweeter syntax, naturally typed as T? var d = args.Argument<decimal>("price");
     Console.WriteLine("price<" + d + ">"); if (!d.HasValue) Console.WriteLine("value missing or format was wrong"); }

     

    Now the extension methods

     

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Extensia.Console { using Core; public static class Extensions { public static bool Argued(this string[] arguments, string parameter) {
     return arguments.Contains("/" + parameter); } public static string Argument(this string[] arguments, string parameter) { var x = (string)null; if (arguments != null && parameter != null) { var p = "/" + parameter + ":"; var q = from arg in arguments where
     arg.Contains(p) && arg.Length > p.Length select arg.Substring(p.Length); x = (q.Count() > 0) ? q.First<string>() : null; } return x; } public static T? Argument<T>(this string[] arguments, string parameter) where T : struct { return arguments.Argument(parameter).Eval<T>();
     } } } 

     

    And a former aquaintance, slightly redesigned due to the mess that is exceptions

     

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Extensia.Core { using System.ComponentModel; public static class Extensions { public static T? Eval<T>(this object x) where T : struct
     { var z = (T?)null; if (x != null) { var ct = TypeDescriptor.GetConverter(typeof(T)); if (ct.CanConvertFrom(x.GetType())) { try { z = (T)ct.ConvertFrom(x); } catch (Exception e) { var safe = (e.GetBaseException() is FormatException) || (e.GetBaseException()
     is InvalidCastException); if (!safe) throw e; } } } return z; } } }

     

    Enjoy Smiley

  • Extensia

    Bart de Smet, one of the most brilliant bloggers I've seen, has a cool extension method in his Memoization for Dummies post.

     

    public static Func<S, T> Memoize<S, T>(this Func<S, T> f) { var cache = new Dictionary<S, T>(); return s => { T t; return cache.TryGetValue(s, out t) ? t : cache[s] = f(s); }; } 

     

    Slightly rewritten.

  • Extensia

    stevo_ said:

    Err, is this thread screwed for anyone else? (for me the second page is called page 1, but contains some of page 2 and 1 posts bashed together)

    Not here, using developer branch of Chrome though. So might be screwed tomorrow, might not.

  • Splinter Threads

    Allow the thread owner to spinter a thread if it steers off-topic. This means cutting off the off-topic part (and any post quoting it) and injecting it into a new thread.

  • A sweeter XLinq

    W3bbo said:
    joechung said:
    *snip*

    Please no. I like C# 2.0's clean syntax. C#3.0's addition of Linq and Lamba methods muddied the waters a bit, but no. VB.NET should be the language saddled with all these gimmicks and domain-specific functionality, leave C# as a pure systems programming language.

    C# is a systems programming language?

     

    Why does LINQ and Lambda expressions muddy the waters?

     

    If anything they purify the waters and C# is more of an applications programming language, albeit with unsafe constructs.

     

    Splinter-thread formed to keep this thread pure.

  • A sweeter XLinq

    Sven Groot said:
    exoteric said:
    *snip*

    Which is why I'm saying that VB's native XML support is better than the dynamic approach in C#, because it doesn't have that problem, and you can easily use namespaces and such as well.

     

    Didn't they recently say that VB and C# will be developed in tandem in the future? Wouldn't that mean that VB's XML support is also going to come to C#?

    In theory yes but I still doubt XML support. Let's see...

     

    I sort of like the idea that you can import syntax locally such that you could say "using System.Numeric.Syntax", or some such, and apply special syntax in a pair of brackets.

  • A sweeter XLinq

    Sven Groot said:
    exoteric said:
    *snip*

    Which is why VB's XLinq uses .<element> (and .@attribute for attributes), so it doesn't look the same as a regular property. Smiley

    That's sweet indeed but that's not the problem I'm talking about. I'm talking about mapping a name such as "my-book_Store" to an XML method name in a dynamic. I can't see how this could work. Yes you could restrict your names to C# compatible method names, but...

  • A sweeter XLinq

    Sven Groot said:

    VB actually already allows you to do that:

     

    Return From d In result.<reply>.<order-list>...<order-detail> _
           Select New Order() With _
           { _
               Confirmed = CBool(d.<confirmed>.Value), _
               ExecutionDate = CDate(d.<execution-date>.Value), _
               ExecutionTimeFrom = CDate(d.<execution-time-from>.Value), _
               ExecutionTimeTo = CDate(d.<execution-time-to>.Value) _
           }


    Heh, my syntax highlighter gets confused because some of these things are keywords, even though the actual VB parser will know that they're not in that context (the VB grammar is a horribly complicated context-sensitive grammar, while my syntax highlighter used on C9 just uses a regex). Smiley

    Yes, VB has built-in XML syntax support. I both like and dislike this (like the usefullness, dislike baking in XML support directly). It's cool syntax though, really nice. (I should have used Elements(...) in the example btw).

  • A sweeter XLinq

    TommyCarlier said:

    C# 4.0 has the dynamic keyword that will enable something like Dynamic XML, that will let you code like this:

    return
      from d in result.reply.orderList.orderDetail
      select new Order()
      {
        Confirmed = d.confirmed,
        ExecutionDate = d.executionDate,
        ExecutionTimeFrom = d.executionTimeFrom,
        ExecutionTimeTo = d.executionTimeTo
      };

    (I'm not sure how the names with the dashes would translate)

    I thought about Dynamic but don't like dot syntax for XML since XML element names do not match up with method name syntax.