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
  • Closest Ancestor Matching Condition

    xml csharp com question
    1
    0 Votes
    1 Posts
    4 Views
    No one has replied
  • T-SQL LOOP

    database help question
    12
    0 Votes
    12 Posts
    37 Views
    S
    I had the exact same problem about a week ago except mine was with an integer that was keeping the previous row's value if the current row was null. You would think that all variables in the scope of the while loop would be null for each iteration.
  • Bizarre error with Borland C++ and GetUserName

    help c++ delphi design tutorial
    9
    0 Votes
    9 Posts
    29 Views
    D
    I think a generally safer way is to ask for the required size, allocate the memory, and then provide the buffer to store the value. e.g. char* name = NULL; DWORD namesize = 0; //Ask for required size GetUserName(NULL, &namesize); name = (char*)malloc(namesize+1); GetUserName(name, &namesize); //Do whatever... free(name); ...Plug & Pray... X|
  • A P/Invoke mystery [modified]

    csharp c++ css data-structures performance
    9
    0 Votes
    9 Posts
    31 Views
    P
    Always use System.Runtime.InteropServices.Marshal.SizeOf method to ensure platform independency. With great code, comes great complexity, so keep it simple stupid...:-\ :-\
  • String with \r in the end

    help c++ debugging json
    14
    0 Votes
    14 Posts
    38 Views
    M
    Not a problem if the program was well written; for example robocopy has the /np option for exactly that purpose. Matt Gerrans
  • Strongly-typed typo

    14
    0 Votes
    14 Posts
    63 Views
    D
    nothing to do with IDEs in this one. you could write that code in vi and have the same problem :)
  • no iterations

    question php com beta-testing
    22
    0 Votes
    22 Posts
    78 Views
    H
    I must say I was rather pleased with myself at having worked out the correct answer before reading this post (and another) with the correct answer in. Of course if anybody actually implemented this in production code they would be told off. Reminds me of the pointer trickery fun we used to do in C "for performance reasons" :)
  • Funny strings

    c++ csharp visual-studio debugging
    11
    0 Votes
    11 Posts
    24 Views
    H
    VS has a warning (C4837) for this as well - by default (pre vs2010) was off by default. We have (as part of our standard headers that apply to all compile units) a set of warnings we always turn on (and some we turn off). Somebody had slipped that one in a while back :)
  • 0 Votes
    5 Posts
    13 Views
    K
    Thank you very Much, I tried to add selfSLL to IIS, then I can't connect to sql server. “A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) (type SqlException)" Your instruction solve my problem. Karay AKAR - Yapar
  • Parallelizing code the easy (er) way

    csharp tutorial
    22
    0 Votes
    22 Posts
    129 Views
    I
    thanks intresting
  • Location Change

    8
    0 Votes
    8 Posts
    19 Views
    K
    you got it :) Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
  • ObservableTable

    csharp database wpf linq com
    1
    0 Votes
    1 Posts
    6 Views
    No one has replied
  • Incrementing numbers

    help question
    19
    0 Votes
    19 Posts
    53 Views
    L
    V. wrote: I prefer number++; return number; I prefer: return number + 1; :doh: xacc.ide IronScheme - 1.0 RC 1 - out now! ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition
  • Trigraphs and C++

    c++ design debugging help question
    7
    0 Votes
    7 Posts
    27 Views
    L
    I had come across this issue some years ago when trying to write C code from an IBM 3270 (?) terminal. IBM's keyboard was missing a few of the characters needed so the trigraph trick was the solution. I didn't realise it had actually become part of the language. It's time for a new signature.
  • Wrap your head around this.

    question
    16
    0 Votes
    16 Posts
    43 Views
    C
    Appears to be a Fibonacci sequence.
  • append extra fields to existing objects

    6
    0 Votes
    6 Posts
    16 Views
    M
    internal static XsdErrorListAttribute getFromObject(object o) { return TypeDescriptor.GetAttributes(o).OfType<XsdErrorListAttribute>().FirstOrDefault(); } does the same, or am i wrong?
  • [PoweShell] Extract all sources

    csharp docker data-structures tools
    2
    0 Votes
    2 Posts
    8 Views
    L
    Please leave a commentary before giving a 1 vote. X| The script is overcomplicated "a bit", but hey it's wicked isn't it? :-\ Greetings - Jacek
  • The magicians big secret

    question com testing beta-testing help
    11
    0 Votes
    11 Posts
    36 Views
    B
    that is from the walls of the cave
  • Enumerators

    json help question
    27
    0 Votes
    27 Posts
    70 Views
    S
    While using these methods I found a shortcoming... there is no support for flags! So this is how I improved it: public static bool TryParseEnum(object value, out EnumType result) { result = default(EnumType); if (value == null) return false; var type = typeof (EnumType); if (!type.IsEnum) return false; int iValue; return Int32.TryParse(value.ToString(), out iValue) ? TryParseEnum(iValue, out result) : TryParseEnum(value.ToString(), out result); } public static bool TryParseEnum(int value, out EnumType result) { result = default(EnumType); var type = typeof(EnumType); if (!type.IsEnum) return false; result = (EnumType)Enum.Parse(type, value.ToString()); // Verify if the result is valid, // Enum.Parse might just return the input value, while this is not a defined value of the enum if (result.ToString().Contains(", ") || Enum.IsDefined(type, result)) return true; result = default(EnumType); return false; } public static bool TryParseEnum(string value, out EnumType result) { result = default(EnumType); if (string.IsNullOrEmpty(value)) return false; var type = typeof(EnumType); if (!type.IsEnum) return false; value = value.ToUpperInvariant(); try { result = (EnumType)Enum.Parse(type, value, true); return true; } catch (ArgumentException) { return false; } } :edited: Edited the object method to use the two other methods, this should be more reliable for both normal enums and bit flag enums. Casting/parsing values which are non-numeric and non-string values is anyway not possible in .NET. modified on Wednesday, February 10, 2010 6:31 AM
  • This one has to become famous: a ghost bug

    c++ java asp-net linux algorithms
    2
    0 Votes
    2 Posts
    6 Views
    B
    :confused: