Note that there are some explanatory texts on larger screens.

plurals
  1. POif statement, boolean value
    primarykey
    data
    text
    <p>This method is taken from Murach's C# 2010 book and is given as an example of an method that checks if a string contains a decimal value:</p> <pre><code> // the new IsDecimal method public bool IsDecimal(TextBox textBox, string name) { //make sure the string only contains numbers and numeric formatting string s = textBox.Text; int decimalCount = 0; bool validDecimal = true; foreach (char c in s) { if (!( c == '0' || c == '1' || c == '2' || // numeric chars c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '.' || c == '$' || c == '%' || c == ',' || // formatting chars c == ' ' )) { validDecimal = false; break; } if (c == '.') { decimalCount++; } } // end loop if (validDecimal &amp;&amp; decimalCount &lt;= 1) { return true; } else { MessageBox.Show(name + " must be a decimal number.", "Entry Error"); textBox.Focus(); return false; } } </code></pre> <p>My question is about this if statement and boolean value:</p> <pre><code> if (validDecimal &amp;&amp; decimalCount &lt;= 1) { return true; } </code></pre> <p>I understand that it should be checking if both validDecimal returns true from above loop and that there is only one decimal point present. I'm quite confused by this, my understanding of bool is that it can hold two values: 1 = true and 0 = false; in both of those cases that if statement would be satisfied (0 is smaller than 1 and 1 is equal). I'm thinking that correct if statement would look something like that: if (validDecimal == true &amp;&amp; decimalCount &lt;= 1) but since I'm beginner I'm not sure and this isn't in the errata for this book.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload