Showing tag results for Code

Apr 21, 2004
Post comments count0
Post likes count0

Why the compiler can’t autoconvert foreach to for

Raymond Chen

People have discovered that the "natural" C# loop construct is fractionally slower than the corresponding manual loop: The first thing that needs to be said here is that The performance difference is almost certainly insignificant. Don't go running around changing all your foreach loops into corresponding for loops thinking that your progra...

Code
Apr 19, 2004
Post comments count0
Post likes count1

WM_KILLFOCUS is the wrong time to do field validation

Raymond Chen

"I'll do my field validation when I get a WM_KILLFOCUS message." This is wrong for multiple reasons. First, you may not get your focus loss message until it's too late. Consider a dialog box with an edit control and an OK button. The edit control validates its contents on receipt of the WM_KILLFOCUS message. Suppose the user fills in some ...

Code
Apr 16, 2004
Post comments count0
Post likes count0

Mapping all those "strange" digits to "0" through "9"

Raymond Chen

In an earlier article, I discussed how the Char.IsDigit() method and its Win32 counterpart, GetStringTypeEx report things to be digits that aren't just "0" through "9". If you really care just about "0" through "9", then you can test for them explicitly. For example, as a regular expression, use [0-9] instead of \d. Alternatively, for a regular exp...

Code
Apr 14, 2004
Post comments count0
Post likes count2

Not all short filenames contain a tilde

Raymond Chen

I'm sure everybody has seen the autogenerated short names for long file names. For the long name "Long name for file.txt", you might get "LONGNA~1.TXT" or possibly "LO18C9~1.TXT" if there are a lot of collisions. What you may not know is that sometimes there is no tilde at all! Each filesystem decides how it wants to implement short filenam...

Code
Apr 13, 2004
Post comments count0
Post likes count2

Unicode collation is hard

Raymond Chen

The principle of "garbage in, garbage out" applies to Unicode collation. If you hand it a meaningless string and ask to compare it to another meaningless string, you get meaningless results. I am not a Unicode expert; I just play one on the web. A real Unicode expert is Michael Kaplan, whose explanation of how comparing invalid Unicode ...

Code
Apr 12, 2004
Post comments count0
Post likes count1

The random number seed can be the weakest link

Raymond Chen

Random number generation is hard. That's why you should leave it to the experts. But even if you choose a good random number generator, you still have to seed it properly. The best random number generator in the world isn't very useful if people can guess the seed. That's why seeding the random number generator with the current time is...

Code
Apr 8, 2004
Post comments count0
Post likes count1

Where does the taskbar get grouped button titles from?

Raymond Chen

If the "Group similar taskbar buttons" box is checked (default) and space starts to get tight on the taskbar, then then the taskbar will group together buttons represending windows from the same program and give them a common name. Where does this common name come from? The name for grouped taskbar buttons comes from the version resource of ...

Code
Apr 6, 2004
Post comments count0
Post likes count2

Reference counting is hard.

Raymond Chen

One of the big advantages of managed code is that you don't have to worry about managing object lifetimes. Here's an example of some unmanaged code that tries to manage reference counts and doesn't quite get it right. Even a seemingly-simple function has a reference-counting bug. The point of this function is to take a (pointer to) a variable t...

Code
Mar 25, 2004
Post comments count0
Post likes count0

Regular expressions and the dreaded *? operator

Raymond Chen

The regular expression *? operator means "Match as few characters as necessary to make this pattern succeed." But look at what happens when you mix it up a bit: This pattern matches a quoted string containing no embedded quotes. This works because the first quotation mark starts the string, the .*? gobbles up everything in between, and th...

Code
Mar 22, 2004
Post comments count0
Post likes count1

Why an object cannot be its own enumerator

Raymond Chen

I've seen people using the following cheat when forced to implement an enumerator: Why create a separate enumerator object when you can just be your own enumerator? It's so much easier. And it's wrong. Consider what happens if two people try to enumerate your formats at the same time: The two enumerators are really the same enumerator, so o...

Code