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
  • Ubiquitous apr_bucket errors. [modified]

    database tools
    1
    0 Votes
    1 Posts
    5 Views
    No one has replied
  • Memory allocation woes

    12
    0 Votes
    12 Posts
    33 Views
    S
    Gary R. Wheeler wrote: Bit-twiddling in VB just seems like you're asking for trouble. The same problem exists in C. If I were writing a language spec, I would specify that the 'not' operator shall always behave as though it returned the longest existing integer type, and that signed/unsigned comparisons shall always behave in numerically-correct fashion (i.e. if the signed number is negative, it's less than the signed one; otherwise the values compare numerically). Actually, I'd specify that any integer expression all of whose whose intermediate subexpressions fit in the largest integer type must be evaluated as though all calculations were done in that largest type. In many cases, a halfway-intelligent compiler should be able to figure out what size operands are actually necessary, and I can't think of any case where such behavior would break decently-written code. Do you know of any languages that work that way? BTW, a couple more things: (1) a decent compiler should be able to recognize the cases where casting to a long doesn't really mean casting to a long, such as "longvar = int1 * (long)int2;" or "longvar &= ~2;". In the former case, the hardware should use one int*int->long multiply rather than sign extending the two integers, performing four uint*uint->ulong multiplies, and adding up the partial products; a decent compiler should also recognize "longvar &= ~(long)smallpositivevalue;" and not bother doing anything with the upper bits of longvar; (2) I wonder why more languages and CPUs don't have an "and not" operator/instruction. I think Vax Basic included such an operator, and the ARM instruction set does, but I've not seen them often. One more thing: I think it's cool that the formula for computing sum(i=0..inf)(2^i) yields -1. So computers and "real math" agree.
  • 0 Votes
    30 Posts
    118 Views
    T
    I'm from the UK and it doesn't happen for me either :p FYI: if you used FXCop, it would remind you to supply the CultureInfo ;-)
  • Nothing big, just a small LINQ query [modified]

    database csharp linq security
    10
    0 Votes
    10 Posts
    29 Views
    P
    I'll occasionally have to add such things to debug, but then I remove them again.
  • Silverlight huh?

    9
    0 Votes
    9 Posts
    22 Views
    P
    To a large extent, proper coercion on a DP takes care of this. The problem however, is that you have no indicator on a DP that coercion is implemented. It's purely optional, which can be a real PITA to sort. Good post though. "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes. My blog | My articles | MoXAML PowerToys | Onyx
  • local variables in EF Linq query

    5
    0 Votes
    5 Posts
    12 Views
    N
    From Microsoft support: This is happening because SharePoint uses its own version of “Medium” trust, which does not enable reflection. The ASP.NET medium trust configuration was changed when Visual Studio 2008 was released to allow reflection in very specific cases. LINQ to SQL and Entity Framework need this permission in order to query in the way that you want. To make this work in your specific scenario, you can add the following in your SharePoint wss_mediumtrust.config file (on my machine it is located in %CommonProgramFiles%\Microsoft Shared\Web Server Extensions\12\config\wss_mediumtrust.config). The only difference is that this will enable code to do reflection to discover non-public members in assemblies that are in the same trust level (i.e. SharePoint Medium trust). (This is a simplification; it’s known as RestrictedMemberAccess, and there is more info here[^].) wss_mediumtrust.config Add the following in the SecurityClasses <SecurityClass Name="ReflectionPermission" Description="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> Then add the following in PermissionSet <IPermission class="ReflectionPermission" version="1" Flags="RestrictedMemberAccess"/> only two letters away from being an asset
  • Subtle crashes [modified]

    question
    12
    0 Votes
    12 Posts
    35 Views
    G
    COM does indeed sucketh, but sinceth it's the only game in town for certain things, get used to the vacuum. Software Zen: delete this; Fold With Us![^]
  • 0 Votes
    9 Posts
    28 Views
    J
    Without specifying the type, the C# compiler will turn 2 into an Int32. It will box the int into the object x. You them attempt to unbox the int into a Nullable. This won't work and will throw a runtime exception. You can only unbox a value into its original value. Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
  • Writing compiler, got obfuscator?

    question csharp c++ dotnet graphics
    7
    0 Votes
    7 Posts
    22 Views
    J
    gesus! writting a complier must be a difficult job!
  • doubles a == b or not ?

    csharp help question
    33
    0 Votes
    33 Posts
    152 Views
    S
    However, as long as the statement is not forced into a double, the value is computed with 80bit precision, thereby correctly evaluating to 0.0 in all cases given?! Interesting point; I'd forgotten that the 80-bit type has a larger exponent range as well as a larger mantissa, and thus that the operation that would overflow a type 'double' won't overflow the internal-precision type. That does raise a question, though, of why there is no facility to store the extended-precision type anywhere? I understand that given word-alignment issues it may not be practical to store a packed 10-byte type, but I would think it would have been useful to have a 16-byte floating-point type which is guaranteed to hold any floating-point number to full internal representation (on machines with 80-bit floats, six bytes would be unused). Programmers would be encouraged to use type 'double' for long-term data storage, but would be able to use the extended precision for calculations where it matters.
  • Regular Expression trouble

    regex learning
    2
    0 Votes
    2 Posts
    8 Views
    M
    Or [-+/] or [+/-]. - at the start or end of the group is documented to just match '-'. Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)
  • When does 3 Nov = March 11?

    question
    10
    0 Votes
    10 Posts
    28 Views
    V
    WTF? Nobody in their right mind would use yyyy-dd-MM. :wtf: It's bad enought the Americans use the middle-Endian MM-dd-yyyy, but MS screws up the standard xxxx-xx-xx by having a yyyy-dd-MM interpretation? :doh: Cheers, Vıkram. Carpe Diem.
  • Just a short story

    help mobile com design testing
    4
    0 Votes
    4 Posts
    13 Views
    M
    fter hours and hours of searching for the error, it's ussually just a single line Happens to all of us.
  • Deleting Rows and Columns in DataGridView

    csharp html database com tools
    7
    0 Votes
    7 Posts
    20 Views
    M
    5...minutes? Seriously, most of us brood an hour or two over or programs before we see what'as wrong. 5 minutes is not worth mentioning.
  • C# rots your C++ brain part 2

    csharp c++ question
    22
    0 Votes
    22 Posts
    83 Views
    J
    It does not matter if you use the parenthesis or not, the default constructor is called anyway. I tend to use parenthesis (how the hell do you pluralize that word? :)), because it makes the syntax more uniform. There are some odd scenarios though, where the parenthesis can fool the compiler into believing that you're declaring a function prototype. Can't remember exactly how to provoke it though... -- Kein Mitleid Für Die Mehrheit
  • LINQ gotcha I ran into

    help csharp linq announcement
    13
    0 Votes
    13 Posts
    34 Views
    A
    gajatko wrote: There is no ?: in VB, soif (a == 0) return 1;else return a - b; VB has the IIf[^] function ("Inline If"), which is not short-circuited like the ternary operator, but for this simple case works just fine: Array.Sort(number, Function(a, b) IIf(a = 0, 1, a - b)) ShamWow
  • C# rots your C++ brain [modified]

    question csharp c++ testing beta-testing
    7
    0 Votes
    7 Posts
    20 Views
    T
    Ha, I've been doing vb.net for the last 5 years. My brain is now as rotten as they come. I wouldn't know a curly bracket if it hit me in the face anymore. TadMcClellan.Com
  • MySQL not liking certain "alias_name"

    database mysql testing beta-testing help
    8
    0 Votes
    8 Posts
    27 Views
    S
    Will do, but using the apostrophe near the enter key worked fine.. not sure if it'll raise problems down the road? I can use the other instead I guess.
  • Subtle SQL Error

    csharp database help question
    5
    0 Votes
    5 Posts
    20 Views
    S
    you have an entire table for what is essentially 1 column? Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. -Fred Brooks
  • C# Win API ExtFloodFill Issue

    graphics csharp algorithms regex json
    5
    0 Votes
    5 Posts
    12 Views
    R
    hi, i am curious how you went with this, i keep getting false from ExtFloodFill