944,147 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 8063
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 4th, 2006
0

Custom error checking

Expand Post »
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
C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Apr 4th, 2006
0

Re: Custom error checking

Maybe somthing like the Masked textbox?

http://www.c-sharpcorner.com/Code/20...BoxControl.asp
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Apr 5th, 2006
0

Re: Custom error checking

Or just put that code into a try/catch block and output the error message or a custom message
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Apr 5th, 2006
0

Re: Custom error checking

Quote 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:

C# Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 5th, 2006
0

Re: Custom error checking

In that case this is how I would do it like this.

C# Syntax (Toggle Plain Text)
  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.

C# Syntax (Toggle Plain Text)
  1. IsNumeric("4");

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

C# Syntax (Toggle Plain Text)
  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 .
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Apr 6th, 2006
0

Re: Custom error checking

Refining tayspen's example a bit ( i hope he doesn't mind):
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Apr 6th, 2006
0

Re: Custom error checking

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


C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Nov 19th, 2007
0

Re: Custom error checking

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.


C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nivek564 is offline Offline
3 posts
since Nov 2007
Nov 19th, 2007
1

Re: Custom error checking

Click to Expand / Collapse  Quote originally posted by nivek564 ...
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.


C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Nov 19th, 2007
0

Re: Custom error checking

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nivek564 is offline Offline
3 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Wrapping a text in Textbox
Next Thread in C# Forum Timeline: I want any help in this problem please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC