aewarnick Posted February 1, 2003 Posted February 1, 2003 if (x != 1-9999999) {} How would I write that? Quote C#
*Experts* Volte Posted February 1, 2003 *Experts* Posted February 1, 2003 if ((x < 1) || (x > 9999999)) { MessageBox.Show("x is not between 1 and 9999999"); } Quote
aewarnick Posted February 1, 2003 Author Posted February 1, 2003 (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 February 1, 2003 by aewarnick Quote C#
*Experts* Volte Posted February 1, 2003 *Experts* Posted February 1, 2003 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 } } Quote
aewarnick Posted February 1, 2003 Author Posted February 1, 2003 That works perfectly! How does if know to execute the loop if true or false is returned without putting: if (isNum(x.Text)==true) Quote C#
*Experts* Volte Posted February 1, 2003 *Experts* Posted February 1, 2003 (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 February 1, 2003 by Volte Quote
aewarnick Posted February 1, 2003 Author Posted February 1, 2003 Very, Very good explanation. Thank you!! Quote C#
*Experts* Volte Posted February 1, 2003 *Experts* Posted February 1, 2003 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 } Quote
aewarnick Posted February 1, 2003 Author Posted February 1, 2003 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)){}??? Quote C#
*Experts* Volte Posted February 1, 2003 *Experts* Posted February 1, 2003 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). Quote
aewarnick Posted February 1, 2003 Author Posted February 1, 2003 I'll try it out later. I am gathering all this info so that I don't forget it. Thank you. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.