Skip to content

Clever Code

Showcase your best code, your most elegant solution, or your most hard fought subtle bug you've found and fixed

This category can be followed from the open social web via the handle clever-code@forum.codeproject.com

361 Topics 3.2k Posts
  • Queue through all files in a directory (and subdirectories).

    data-structures
    12
    0 Votes
    12 Posts
    44 Views
    C
    No biggie. Recursion would be ideal for this scenario. ;)
  • My Far Call

    csharp asp-net wpf wcf data-structures
    2
    0 Votes
    2 Posts
    14 Views
    J
    That may be clever, but I don't think it qualifies as 'code' ;P 'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood
  • Did I St-St-Stutter?

    csharp asp-net sysadmin help question
    26
    0 Votes
    26 Posts
    117 Views
    T
    Exactly! It's very truly said! :thumbsup:
  • Hidden Features of C#

    csharp com question
    25
    0 Votes
    25 Posts
    117 Views
    T
    What is RTFM :cool:: --- With regards... The nk.
  • MatchList

    regex
    6
    0 Votes
    6 Posts
    28 Views
    E
    Recommendation: replace List with IEnumerable (you will need to add parens after .Count) - this way you can use this method to compare not just Lists but also other collection types, and even arrays!
  • Useful thingy

    csharp database com question
    5
    0 Votes
    5 Posts
    22 Views
    B
    Hi Jacek, Interesting, I thought I had been using 'HasFlag on the MouseButtons Enum in Windows Forms previous to .NET 4.0. Also interesting about 'HasFlag is that it will work even on Enumerations that are not decorated with the [Flags] Attribute, while the MS documentation implies, in my reading, that it only works with same: "The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute." // from the MS doc for Enum.HasFlag // no \[Flags\] attribute ! public enum DinnerItems { None = 0, Entree = 1, Appetizer = 2, Side = 4, Dessert = 8, Beverage = 16, BarBeverage = 32 } // execute this somewhere in your code DinnerItems thisMeal; // This will also compile without error in .NET 4.0 ! // DinnerItems thisMeal = new DinnerItems(); thisMeal = DinnerItems.Dessert; if (thisMeal == DinnerItems.Dessert) MessageBox.Show("woof woof"); if(thisMeal.HasFlag(DinnerItems.Dessert)) MessageBox.Show("meow"); The comment on the MSDN page you linked to by a "community member" that using 'HasFlag is "1000 times slower" than the "standard method" is kind of scary. best, Bill "Reason is the natural order of truth; but imagination is the organ of meaning." C.S. Lewis
  • Clever Code

    com question learning
    2
    0 Votes
    2 Posts
    6 Views
    G
    Set the date filter for the forum to "All" and it will look a lot more reasonable. Software Zen: delete this;
  • Interpreting a nullable datetime in an input file

    question
    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • Subtle Javascript Bug

    javascript help question
    10
    0 Votes
    10 Posts
    27 Views
    J
    omg, that is a very clever habit to get into! I love it - and am almost ashamed I haven't though to do that...that little typeo gets me every now and then ________ John Y. Developer
  • Animated dots? No problem!

    help question
    5
    0 Votes
    5 Posts
    15 Views
    L
    OK I get it. It took me long though. :(( :(( :(( :(( :laugh: Greetings - Jacek
  • 0 Votes
    10 Posts
    29 Views
    L
    The C# code is actually wrong, C# doesn't support the My namespace (it is automatically generated by VB.NET).
  • Almost a complete waste of time

    javascript game-dev help question
    8
    0 Votes
    8 Posts
    20 Views
    J
    Wow this looks like an interesting project!
  • Template Template Template Parameter

    java database com design question
    10
    0 Votes
    10 Posts
    24 Views
    U
    i really dont know how to add this to bling h.p. and im trying every i can think of. can u help?:confused:
  • 0 Votes
    13 Posts
    34 Views
    K
    Dave Kreskowiak wrote: You design the solution to fit the problem and it's scale. That part I totally agree with. I did say "using datareader to read megs of rows." That's much bigger scale than the original poster indicated. Hopefully when you get to that scale, you handle your problems piecemeal.
  • Methods within methods

    css question
    12
    0 Votes
    12 Posts
    32 Views
    P
    Well, exactly this is the reason, why C# was invented... :)
  • Metafiles and Word

    graphics help algorithms data-structures business
    2
    0 Votes
    2 Posts
    6 Views
    M
    Yep! There's nothing like a demonstration with you customer to make these little devils rear their heads. :sigh:
  • Evil Enumerations

    csharp database linq com data-structures
    5
    0 Votes
    5 Posts
    14 Views
    J
    What a weird response.
  • Suppressing modal popup after first view

    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • 0 Votes
    4 Posts
    14 Views
    K
    Ian Shlasko wrote: So what do you think? Too concise? Too LINQy? Or just right absolutely right! i also prefer C# coding like this...it makes more like a functional language while keeping the good ol' imperative stuff. my colleagues are slowly going to use these neat feautures :>
  • Here's something else VB can't do

    debugging
    21
    0 Votes
    21 Posts
    85 Views
    L
    Funny, I just did that in my C++ code the other day. I just had to experiment and you are correct, what is simple in C/C++ will never work in VB. Nice technical point. I am far more used to C and C# programming and starting a VB contract job next week. This is a good heads up for me. Thanks. I tried this is VB in VS2008 as a console app and it seems more or less equivalent to your sample: Module Module1 Sub Main() Dim iNumber As Integer Dim tText As String iNumber = 99 tText = "Is iNumber less than 100?" If (iNumber < 100) Then tText = "iNumber is less than 100" #If DEBUG Then iNumber = iNumber + 2 <<<<<<<<<<< GETS HERE FINE IN DEBUG. BUT, GOES FROM HERE TO End If #End If #If DEBUG Then Else tText = "iNumber is more than 100" <<<<<<<<<<< NEVER RUNS THIS CODE iNumber = iNumber + 7 #End If iNumber = iNumber - 19 <<<<<<<<<<< NEVER GETS HERE, JUST SKIPS IT End If Console.WriteLine(tText) Console.WriteLine(iNumber) End Sub End Module OUTPUT: iNumber is less than 100 101 TW Burger