Declaring SIXTH_BIT_CLEAR using hexadecimal at least tips experienced programmers off that this is not a normal number. It's something special such as a bit mask (as is the case here). It would be better if we could specify the value in binary (11011111) but that's not an option in Visual Basic.
5. Now is this a good idea?
This is much better but it's still not a great idea. The code is still obscure and relies on the sneaky trick that clearing the 6th bit converts from lowercase into uppercase so it won't work if the character set changes.
The original tip I saw touted this trick as faster than using Chr$, UCase$, and Asc. In fact, it is faster. In one informal test, the original version took approximately 0.00000077 seconds to convert a character into uppercase. The version using Chr$, UCase$, and Asc took about 0.00000340 seconds.
However, the code is used in a KeyPress event handler so it executes when the user types a character. Unless your user can type a several thousand characters per second, the difference in speed isn't going to matter much.
Amusingly the original tip also said to put the code in a subroutine if you need to use it in multiple TextBoxes. That roughly doubles the time. If you really need those extra microseconds, this is a bad idea.
Moral
Initially write your code in a straightforward way. Use comments, constants, and other good programming practices to make the code easy to understand.
Later, if you know you have a problem with the code's performance, you can optimize it. In this example, there will be no problem because the code is in a KeyPress event handler and the user's typing speed will make the tiny improvement in speed moot. Don't waste time optimizing code that is fast enough.
Get it working correctly first. Then optimize only where you know it is necessary. (This is an important theme in my book Bug Proofing Visual Basic).
As a final note, most software projects fail because they don't correctly do what they are supposed to do, not because they do the right thing too slowly!
|