Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

Edited by aewarnick
C#
  • *Experts*
Posted

Put the following function in your code:

private bool isNum(string str) 
{
try   { int dummy = int.Parse(str); }
catch { return false; }
return true;
}

And then you can use

if (isNum(x.Text))
{
 int num = int.Parse(x.Text);
 if ((num < -1)||(num > 9999999))
 {
   // it's valid
 }
}

  • *Experts*
Posted (edited)

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

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,

if (someBool == true)

is evaluated as

if ((someBool == true) == true)

and that's not really very efficient.

 

[edit]Oooops... I meant "x == 6". not "x = 6"[/edit]

Edited by Volte
  • *Experts*
Posted

You're 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:

if (!someBool)
{
 //someBool is false
}

Posted

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)){}???

C#
  • *Experts*
Posted

Well, there is another way you can do it with looping through every

character. For example:

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 it's numeric, while

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 it's letters only. Just change the 'IsLetter'

function to one of the other checks (they are listed in MSDN and the

intellisense).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...