if (x != 1-9999999) {}

C#:
if ((x < 1) || (x > 9999999)) 
{
	MessageBox.Show("x is not between 1 and 9999999");
}
 
I said that wrong. Here is what I am really trying to do. I have a text box called x and I have a picture that the user clicks on to calculate something but if they enter in anything out of range or a character the program fails. How do I prevent it?

if ((float.Parse(x.Text) >- 1)||(float.Parse(x.Text) < 9999999))

The probelm with that is if the user enters in a character it cannot convert it safely. How do I check to see if what was entered was a character or number?
 
Last edited by a moderator:
Put the following function in your code:
C#:
private bool isNum(string str) 
{
	try   { int dummy = int.Parse(str); }
	catch { return false; }
	return true;
}
And then you can use
C#:
if (isNum(x.Text))
{
  int num = int.Parse(x.Text);
  if ((num < -1)||(num > 9999999))
  {
    // its valid
  }
}
 
That works perfectly!

How does if know to execute the loop if true or false is returned without putting:

if (isNum(x.Text)==true)
 
Ah, booleans can be pretty confusing for someone new to programming.

If statements operate solely on booleans; the block of code in the "if"
is only executed if the condition in the "if" statement is true.
The condition in the "if" is always a boolean. For
example, in the code
C#:
if (x == 6)
"x == 6" is a boolean. When you use a comparison operator in an
expression, the expression will be evaluated and return true
or false based on, obviously, whether the expression is true
or false.

So basically, if (x == 6) is evaluated as if (true) is x does indeed
equal 6, or if (false) otherwise. When evaluating a boolean
variable, it automatically returns true or false without needing
a comparison operator.
Essentially,
C#:
if (someBool == true)
is evaluated as
C#:
if ((someBool == true) == true)
and thats not really very efficient.

[edit]Oooops... I meant "x == 6". not "x = 6"[/edit]
 
Last edited by a moderator:
Youre welcome. :)

One other thing I forgot to mention is that if you want to
only execute code if a boolean is false, then you would
do this:
C#:
if (!someBool)
{
  //someBool is false
}
 
What if I come accross a situation where I can only test to see if a user enters in characters or numbers but I cannot test by using try/catch.

if (textbox1==(char)){}???
if (textbox1==(int)){}???
if (textbox1==(double)){}???
 
Well, there is another way you can do it with looping through every
character. For example:
C#:
private bool isNum(string str) 
{
	char[] chr = str.ToCharArray();
			
	foreach (char dummy in chr)
	  if (!char.IsDigit(dummy)) return false;
          
	return true;
}
Is another way to check to see if its numeric, while
C#:
private bool isChar(string str) 
{
	char[] chr = str.ToCharArray();
			
	foreach (char dummy in chr)
	  if (!char.IsLetter(dummy)) return false;
          
	return true;
}
is a way to test to see if its letters only. Just change the IsLetter
function to one of the other checks (they are listed in MSDN and the
intellisense).
 
Back
Top