| | |
Custom error checking
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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
or something to that effect.
just wondering if there was the same for C #
here is what i am talking about:
on the top section where it says
i want to write an error check that will output a message if the user does not input a proper integer value
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)
or die mysql_error()
just wondering if there was the same for C #
here is what i am talking about:
C# Syntax (Toggle Plain Text)
//clear the various boxes lstNetAdd.Items.Clear(); txtCIDR.Text = ""; txtSubNetShown.Text = ""; txtBitsBorrow.Text = ""; //pull the value into the variable double dblSubnetsNeeded, dblBitsToBorrow, dblNET4; dblSubnetsNeeded = Double.Parse(txtSubnetsNeeded.Text); //if they only need one subnet what to do if (dblSubnetsNeeded <= 1 || dblSubnetsNeeded >= 256) { //Value to high or too low MessageBox.Show("Please Enter a Value Betweeen 2 and 255", "Error: Value Range", MessageBoxButtons.OK, MessageBoxIcon.Error); } //what to do if they want more than one else { //determin number of bits to borrow dblBitsToBorrow = Math.Log(dblSubnetsNeeded)/Math.Log(2); //Declare Variables to Handle left over bits decimal decBitsToBorrow = Convert.ToDecimal(dblBitsToBorrow), decBitsLeftOver; //Mod out the decimal decBitsLeftOver = (decBitsToBorrow % 1); //if there are decimal places if (decBitsLeftOver > 0) { //remove the decimal places, incriment the bits by one and then convert for display decBitsToBorrow = decBitsToBorrow - decBitsLeftOver; decBitsToBorrow += 1; Convert.ToString(decBitsToBorrow); }//endif //display the bits to borrow txtBitsBorrow.Text = String.Format( "{0:0}", decBitsToBorrow); //display the current subnet dblBitsToBorrow = Convert.ToDouble(decBitsToBorrow); //determin the subnet dblNET4 = 256 - ((Math.Pow(2, 8)) - (Math.Pow (2, (8-dblBitsToBorrow)))); /* the following section will display the first few results of the subnet addy */ //variables for the first subnet, counter and highest subnet double dblFirstNET4 = dblNET4 - dblNET4, dblNETStore = dblNET4, dblHighestSubnet = 255 - dblNET4; //display first and second subnets lstNetAdd.Items.Add( "192.168.1." + dblFirstNET4); lstNetAdd.Items.Add( "192.168.1." + dblNET4); /*end pre-address display*/ /*Now itterate through the last of the subnets*/ //only work untill we are are less than highest subnet while (dblNET4 < dblHighestSubnet ) { //increment the subnet by one and display the subnet dblNET4 = dblNET4 + dblNETStore; lstNetAdd.Items.Add( "192.168.1." + dblNET4); } /*end itteration*/ /*Show CIDR notation and #of Subnets*/ decimal decCIDR = 24 + decBitsToBorrow; txtCIDR.Text += "/"; txtCIDR.Text += String.Format( "{0:0}", decCIDR); txtSubNetShown.Text = Convert.ToString(lstNetAdd.Items.Count); /*end CIDR and subnets*/ }//endif
on the top section where it says
C# Syntax (Toggle Plain Text)
dblSubnetsNeeded = Double.Parse(txtSubnetsNeeded.Text);
Dont forget to spread the reputation to those that deserve!
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes
Member - Alliance of Security Analysis Professionals - Since 2006
Ewido
Tune up windows
Get detailed system information
My Fixes
Member - Alliance of Security Analysis Professionals - Since 2006
•
•
•
•
Originally Posted by _r0ckbaer
Or just put that code into a try/catch block and output the error message or a custom message
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)
string inputString; try { Convert.ToInt32(inputString); } catch (System.FormatException) { Console.Writeline("Please input a number in inputString"); }
...That's pretty simple, but it shows you how to use error checking.
Alex Cavnar, aka alc6379
In that case this is how I would do it like this.
Then if you wanted to test if it was a number or not.
In that case no error would be thrown. Where as if you did this.
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
.
C# Syntax (Toggle Plain Text)
private static void IsNumeric(string input) { try { Convert.ToInt32(input); } catch (Exception error) { MessageBox.Show(error.Message); } }
Then if you wanted to test if it was a number or not.
C# Syntax (Toggle Plain Text)
IsNumeric("4");
In that case no error would be thrown. Where as if you did this.
C# Syntax (Toggle Plain Text)
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
Ewido
Tune up windows
Get detailed system information
My Fixes
Member - Alliance of Security Analysis Professionals - Since 2006
Refining tayspen's example a bit ( i hope he doesn't mind):
C# Syntax (Toggle Plain Text)
private bool IsNumeric(string input) { bool isNum = true; try { int.Parse(input); } catch { isNum = false; } return isNum; }
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
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)
static bool IsaNumber(string num) { double dbl; return double.TryParse(num, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out dbl); }
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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)
try // check if number is a decimal { string i; decValue = checked((uint)System.Convert.ToUInt32(numberinput.Text)); } catch (System.Exception i) { MessageBox.Show("input a decimal"); return; }
Last edited by nivek564; Nov 19th, 2007 at 11:41 am.
•
•
•
•
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)
try // check if number is a decimal { string i; decValue = checked((uint)System.Convert.ToUInt32(numberinput.Text)); } catch (System.Exception i) { MessageBox.Show("input a decimal"); return; }
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.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- CGI + custom error pages... (Perl)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Basic Error Checking (C++)
- How to Perform Disk Error Checking in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C# Forum
- Previous Thread: Wrapping a text in Textbox
- Next Thread: I want any help in this problem please
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing editing enabled encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post print programming radians regex remote remoting resolved. richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update user usercontrol validation view visualstudio webbrowser windows winforms wpf xml






