[dba-VB] ControlChars in C#

Gustav Brock Gustav at cactus.dk
Tue Mar 25 17:34:52 CDT 2008

Hi Shamil

You are right. It was due to old habits that I used the nested if .. and because during my testing it was easier to edit.

As for the local variables they often make code much easier to browse (not in this simple example) and - if you need to assign the variable something else - it will be done at one place only.

As for the curly brackets or not in a simple if statement I've read several places that it is considered good coding practice to _always_ include these - even for one-line code like here:

     if (something)
         e.Handled = true  


     if (something)
         e.Handled = true  

For example, the add-in, CodeIt.Right, corrects this:



>>> shamil at users.mns.ru 25-03-2008 20:22 >>>
Hi Gustav,

Sorry, I missed the logic (it's a storm and snowfall here - my brain cells
could have been influenced by that :))

I should have coded:

private void bindingNavigatorPositionItem_KeyPress(
      object sender, KeyPressEventArgs e)
if (Char.IsControl(e.KeyChar) &&
    e.KeyChar != ControlChars.Back)
       e.Handled = true  

How about that? Still missing something here? 

Notes on coding style above, which might look unusual for VB(A) veterans
(I'm one of them):

1. "Code refactoring gurus" recommend avoiding using local variables - it's
a compiler's job to optimize compiled code...

2. Using 'if' with two conditional expressions "glued with &&" doesn't
introduce any performance hits because if the first condition fails the
second one will not be evaluated...



-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Tuesday, March 25, 2008 9:59 PM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] ControlChars in C#

Hi Shamil

Yes I need all that code. If omitted, any other keypress than backspace will
be "handled" meaning be inactive; thus I can only delete the digits in the
record position selector (filled and updated when you navigate the table)
with the backspace key, not typing any new digits.


More information about the dba-VB mailing list