Custom error checking

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Custom error checking

 
0
  #1
Apr 4th, 2006
Hey, i was wondering if there was a way to do a custom error check. Such as if a user inputs anything other than a number then i would like it to output an error message. short of having to type in every letter and symbol on the keyboard is there a quick and efficient way of doing this?


i know like in php if you run a bad query or something doesnt go quite as planned you can do a
  1. or die mysql_error()
or something to that effect.

just wondering if there was the same for C #

here is what i am talking about:

  1. //clear the various boxes
  2. lstNetAdd.Items.Clear();
  3. txtCIDR.Text = "";
  4. txtSubNetShown.Text = "";
  5. txtBitsBorrow.Text = "";
  6.  
  7. //pull the value into the variable
  8. double dblSubnetsNeeded, dblBitsToBorrow, dblNET4;
  9. dblSubnetsNeeded = Double.Parse(txtSubnetsNeeded.Text);
  10. //if they only need one subnet what to do
  11. if (dblSubnetsNeeded <= 1 || dblSubnetsNeeded >= 256)
  12. {
  13. //Value to high or too low
  14. MessageBox.Show("Please Enter a Value Betweeen 2 and 255", "Error: Value Range", MessageBoxButtons.OK, MessageBoxIcon.Error);
  15. }
  16. //what to do if they want more than one
  17. else
  18. {
  19. //determin number of bits to borrow
  20. dblBitsToBorrow = Math.Log(dblSubnetsNeeded)/Math.Log(2);
  21. //Declare Variables to Handle left over bits
  22. decimal decBitsToBorrow = Convert.ToDecimal(dblBitsToBorrow),
  23. decBitsLeftOver;
  24. //Mod out the decimal
  25. decBitsLeftOver = (decBitsToBorrow % 1);
  26.  
  27. //if there are decimal places
  28. if (decBitsLeftOver > 0)
  29. {
  30. //remove the decimal places, incriment the bits by one and then convert for display
  31. decBitsToBorrow = decBitsToBorrow - decBitsLeftOver;
  32. decBitsToBorrow += 1;
  33. Convert.ToString(decBitsToBorrow);
  34. }//endif
  35.  
  36. //display the bits to borrow
  37. txtBitsBorrow.Text = String.Format( "{0:0}", decBitsToBorrow);
  38. //display the current subnet
  39. dblBitsToBorrow = Convert.ToDouble(decBitsToBorrow);
  40. //determin the subnet
  41. dblNET4 = 256 - ((Math.Pow(2, 8)) - (Math.Pow (2, (8-dblBitsToBorrow))));
  42. /* the following section will display the first few results of the subnet addy */
  43. //variables for the first subnet, counter and highest subnet
  44. double dblFirstNET4 = dblNET4 - dblNET4,
  45. dblNETStore = dblNET4,
  46. dblHighestSubnet = 255 - dblNET4;
  47. //display first and second subnets
  48. lstNetAdd.Items.Add( "192.168.1." + dblFirstNET4);
  49. lstNetAdd.Items.Add( "192.168.1." + dblNET4);
  50. /*end pre-address display*/
  51. /*Now itterate through the last of the subnets*/
  52. //only work untill we are are less than highest subnet
  53. while (dblNET4 < dblHighestSubnet )
  54. {
  55. //increment the subnet by one and display the subnet
  56. dblNET4 = dblNET4 + dblNETStore;
  57. lstNetAdd.Items.Add( "192.168.1." + dblNET4);
  58. }
  59. /*end itteration*/
  60. /*Show CIDR notation and #of Subnets*/
  61. decimal decCIDR = 24 + decBitsToBorrow;
  62. txtCIDR.Text += "/";
  63. txtCIDR.Text += String.Format( "{0:0}", decCIDR);
  64. txtSubNetShown.Text = Convert.ToString(lstNetAdd.Items.Count);
  65. /*end CIDR and subnets*/
  66. }//endif

on the top section where it says
  1. dblSubnetsNeeded = Double.Parse(txtSubnetsNeeded.Text);
i want to write an error check that will output a message if the user does not input a proper integer value
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Custom error checking

 
0
  #2
Apr 4th, 2006
Maybe somthing like the Masked textbox?

http://www.c-sharpcorner.com/Code/20...BoxControl.asp
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Custom error checking

 
0
  #3
Apr 5th, 2006
Or just put that code into a try/catch block and output the error message or a custom message
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Custom error checking

 
0
  #4
Apr 5th, 2006
Originally Posted by _r0ckbaer
Or just put that code into a try/catch block and output the error message or a custom message
That's what I would do. If you're converting a string to a number type, the Convert.ToInt32() method (or any of the other methods in the Convert class) will throw an exception if the string doesn't really contain a number.

Here's something I whipped up really quick as an example. It should work, but I'm still kind of new at this C# business:

  1.  
  2. string inputString;
  3.  
  4. try
  5. {
  6. Convert.ToInt32(inputString);
  7. }
  8. catch (System.FormatException)
  9. {
  10. Console.Writeline("Please input a number in inputString");
  11. }

...That's pretty simple, but it shows you how to use error checking.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Custom error checking

 
0
  #5
Apr 5th, 2006
In that case this is how I would do it like this.

  1. private static void IsNumeric(string input)
  2. {
  3. try
  4. {
  5. Convert.ToInt32(input);
  6. }
  7. catch (Exception error)
  8. {
  9. MessageBox.Show(error.Message);
  10. }
  11. }

Then if you wanted to test if it was a number or not.

  1. IsNumeric("4");

In that case no error would be thrown. Where as if you did this.

  1. IsNumeric("F");

An error would be thrown.

Again, its the almost the same code as alc's its just a little different way to handle the error .
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Custom error checking

 
0
  #6
Apr 6th, 2006
Refining tayspen's example a bit ( i hope he doesn't mind):
  1. private bool IsNumeric(string input)
  2. {
  3. bool isNum = true;
  4. try
  5. {
  6. int.Parse(input);
  7. }
  8. catch
  9. {
  10. isNum = false;
  11. }
  12.  
  13. return isNum;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Custom error checking

 
0
  #7
Apr 6th, 2006
Here is an example of my prefered method.
the double.TryParse

a regex would also be a good idea to put in front of this over a textbox (if you have 2.0). expecially if its like asp.net


  1. static bool IsaNumber(string num)
  2. {
  3. double dbl;
  4. return double.TryParse(num, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out dbl);
  5.  
  6. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: nivek564 is an unknown quantity at this point 
Solved Threads: 0
nivek564 nivek564 is offline Offline
Newbie Poster

Re: Custom error checking

 
0
  #8
Nov 19th, 2007
this is how i would go about error checking in a text box, can also be used the other way around, by changing the i value for a int and the system.convert to a parse.


  1. try // check if number is a decimal
  2. {
  3. string i;
  4.  
  5. decValue =
  6. checked((uint)System.Convert.ToUInt32(numberinput.Text));
  7. }
  8. catch (System.Exception i)
  9. {
  10.  
  11. MessageBox.Show("input a decimal");
  12. return;
  13. }
Last edited by nivek564; Nov 19th, 2007 at 11:41 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Custom error checking

 
1
  #9
Nov 19th, 2007
Originally Posted by nivek564 View Post
this is how i would go about error checking in a text box, can also be used the other way around, by changing the i value for a int and the system.convert to a parse.


  1. try // check if number is a decimal
  2. {
  3. string i;
  4.  
  5. decValue =
  6. checked((uint)System.Convert.ToUInt32(numberinput.Text));
  7. }
  8. catch (System.Exception i)
  9. {
  10.  
  11. MessageBox.Show("input a decimal");
  12. return;
  13. }
This correctly checks if the value is a number but not a decimal.
You are converting the value to an int which cannot have a value past the decimal point unlike the decimal.
So passing ".1" would fail in this even though this is a valid decimal.

Fix this by using Convert.ToDecimal, or fix the comments to correctly say that this is checking for an int.
Last edited by plazmo; Nov 19th, 2007 at 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: nivek564 is an unknown quantity at this point 
Solved Threads: 0
nivek564 nivek564 is offline Offline
Newbie Poster

Re: Custom error checking

 
0
  #10
Nov 19th, 2007
sorry if i caused any confusion but in c# language decimal is 128 bits to represent values within the range 1E-28 to 7.9E+28. which means numbers between79228162514264337593543950335
and -79228162514264337593543950335, not included the numbers to the right of the decimal place. this is just a little snip it from my code to convert hex to dec numbers thats why i have decimal in the comments on the code. so your right it wont pick up anything to the right of decimal place good call on the convert.todecimal.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC