0

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 die mysql_error()

or something to that effect.

just wondering if there was the same for C #

here is what i am talking about:

//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

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

7
Contributors
10
Replies
11
Views
11 Years
Discussion Span
Last Post by Jugortha
0

Or just put that code into a try/catch block and output the error message or a custom message

0

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:

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.

0

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

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.

IsNumeric("4");

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

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 :).

0

Refining tayspen's example a bit ( i hope he doesn't mind):

private  bool IsNumeric(string input)
        {
bool isNum = true;
            try
            {
                int.Parse(input);
            }
            catch
            {
               isNum = false;
            }

return isNum;
        }
0

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

static bool IsaNumber(string num)
		{
			double dbl;
			return double.TryParse(num, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out dbl);
			
		}
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.

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;
                        }
1

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.

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;
                        }

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.

Votes + Comments
Geez, ease up...
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.

0

on the tool box expand Validation tag add a regular expression Validator object press F4 to display the properties grid then click on the Validation expression propertie u find there a list on built in expression from chose one among them and then set the errormessage propertie obj.errormessage = "my error text";

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.