Chapter 23: Creating a Top-Level Program for Groups of Executables

 

Logic Statements

Logic statements are the only decision-making tools a programmer can use in the code he or she generates. These are powerful statements, and they can be written by the programmer such that they include the user (does the user want to follow path A, path B, or path C) or exclude the user (if x is less than 10, follow path A; else follow path B).

Before we discuss logic statements in general, we should discuss some logic statements that are unique to the Visual Studio C# compiler:

  • File.Exists ” This statement allows the programmer to check for the existence of a file. If the file does not exist, some evasive action can be taken.

     // Open file 'Masterlist.dta' to write the data. if(!File.Exists("C:\Program Files\KT\Masterfile.dta")) {   MessageBox.Show("Cannot find file 'Masterfile.dta' to write.");   Close(); } else 
  • The group of bool functions that operate on one member of a char array or an ANSIString (we list the string form only):

    • Char.IsControl(string str, int Index) ” /, \, @, &, | .

    • Char.IsSeparator(string str, int Index) ” for Unicode only; separates footnotes, chapters, etc.

    • Char.IsWhiteSpace(string str, int Index) ” a blank

    • Char.IsPunctuation(string str, int Index) ” period, semicolon, colon , asterisk, double or single quotes, question mark, exclamation point

    • Char.IsSymbol(string str, int Index) ” All Greek characters , etc.

    • Char.IsDigit(string str, int Index) ” 0-9

    • Char.IsNumber(string str, int Index) ” 0-9

    • Char.IsLetter(string str, int Index) ” a-z, A-Z

    • Char.IsUpper(string str, int Index) ” Uppercase character

    • Char.IsLower(string str, int Index) ” Lowercase character

    • Char.IsLetterOrDigit(string str, int Index) ” Letters a-z or A-Z, digits 0-9

    • Char.IsSurrogate(string str, int Index) ” Two 16-bit fields that represent (in Unicode) a character in an international language (like Chinese)

      For example:

       if(Char.IsDigit(textBox2.Text[1])) {} else NumberOfErrors++; 

      Note that in the example above, if textBox2.Text[1] is a digit (0-9), nothing happens. But if that statement is false, the else statement increments the NumberOfErrors (to signal a problem in the data).

  • Be careful about generalizing multiple logic code that defeats your purpose. Take a look at the following statement:

     if((ASCIIChar[mm] >= &&a') && (ASCIIChar[mm] <= 'Z')) 

    It appears to include the complete alphabet (and only the alphabet). Not true. The proper statement is:

     if(((ASCIIChar[mm] >= 'a') && (ASCIIChar[mm] <= 'z')) ||    ((ASCIIChar[mm] >= 'A') && (ASCIIChar[mm] <= 'Z')) 

    because there are characters between the lowercase and uppercase sets. The uppercase letters, A-Z, are assigned hexadecimal (base 16) digits from 41 to 5A. The lowercase letters, a-z, are assigned hexadecimal digits from 61 to 7A. The safest test is this:

     if(Char.IsLetter(ASCIIChar[mm])) 
  • To pass a single character, the logic statement is:

     if(ASCIIChar[mm] == '#') [for example, the character '#'] 

    To exclude a single character, the logic statement is:

     If(ASCIIChar[mm] != '#') 

    where != means not equal.

  • To exclude a contiguous set of characters (like 0-9), you could write this statement:

     if((ASCIIChar[mm] < 0) || (ASCIIChar[mm] > 9)) then do something .... 

    where || is the or symbol.

    But the preferred statement uses the NOT of the above:

     If(Char.IsDigit(ASCIIChar[mm])) {} else {   do something.... } 
  • In multiple-level logic statements you should shy away from the NOT (!) statement because you can easily end up creating a logic path that none of the characters can pass through.

  • Perhaps the most famous glitch in logic statements is this one:

     if(((Char > '0') && (Char < '9')) && ((Char > 'a') && (Char < 'z'))) 

    No character will ever satisfy this logic statement because no character can exist between 0-9 and a-z.

  • The code shown below comes from a typical text checker sequence. The desired output is a year-week number ” two digits for the year, a dash, then two digits for the week, like 04-26. This code may be found in the KT series.

    • Char[0] must be a 0, 1, or 2, to account for the years 2000 through 2020.

    • Char[1] must be a digit, 0-9.

    • Char[2] must be a dash (-).

    • Char[3] must be a digit, 0-9.

    • Char[4] must be a digit, 0-9, except when Char[3] is a 5. In this case, Char[4] must be a 0, 1, or 2 (since char[3] and char[4] can be no larger than 52).

This code is executed every time the user types a character into a text box.

 BS1701:       private void textBox2_TextChanged(object sender, System.EventArgs e) BS1702:       { // Year-Week number.                 // Check that chars 0, 1, 3, and 4 are digits from 0 to 9.                 // Char 0 must be a 0, 1, or 2 (for the years 2000 - 2020).                 // Also check that char 2 is a dash (-). BS1703:         int DP2L = textBox2.Text.Length; BS1704:         int NumberOfErrors = 0; BS1705:         if(DP2L == 1) BS1706:         { BS1707:           if((textBox2.Text[0] >= '0') && (textBox2.Text[0] <= '2')) BS1708:           { } BS1709:           else NumberOfErrors++; BS1710:         } BS1711:         else if(DP2L == 2) BS1712:         { BS1713:           if(Char.IsDigit(textBox2.Text[1])) BS1714:           { } BS1715:           else NumberOfErrors++; BS1716:         } BS1717:         else if(DP2L == 3) BS1718:         { BS1719:           if(textBox2.Text[2] == '-') BS1720:           { } BS1721:           else NumberOfErrors++; BS1722:         } BS1723:         else if(DP2L == 4) BS1724:         { BS1725:           if((textBox2.Text[3]>='0') && (textBox2.Text[3]<='5')) BS1726:           { } BS1727:           else NumberOfErrors++; BS1728:         } BS1729:         else if(DP2L == 5) BS1730:         {                   // Characters [3] and [4] combined can be no larger than 52. BS1731:           if(textBox2.Text[3] == '5') BS1732:           { BS1733:             if((textBox2.Text[4] >= '0') && (textBox2.Text[4] <= '2')) BS1734:             { } BS1735:             else NumberOfErrors++; BS1736:           } BS1737:           else BS1738:           { BS1739:             if(Char.IsDigit(textBox2.Text[4])) BS1740:             { } BS1741:             else NumberOfErrors++; BS1742:           } BS1743:         } BS1744:         if(NumberOfErrors > 0) BS1745:         { BS1746:           MessageBox.Show("Improper 'Year-Week' data string. Try again."); BS1747:           return; BS1748:         } BS1749:       } 

The return in line BS1747 is extremely important! If an error has occurred in the input data, the focus returns to the editBox that is currently being checked. If there are no errors, this procedure is closed.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net