hi guys, i am fairly new to c# and ive gone through a couple of chapters in my book and started to write some of my own script for the sake of practice.
private void calc_Click(object sender, RoutedEventArgs e)
{
int input = int.Parse(number.Text);
if (input < 10)
check.Text = ("the number you entered is less than to 10");
else if (input == 10)
check.Text = ("the number you entered is equal to 10");
else if (input >= 10)
check.Text = ("the number you entered is larger than 10");
else
numberTwo.Text = ("you did not enter a valid number");
bool invalidNumber = (input >=0);
if (invalidNumber)
numberTwo.Text = input.ToString();
else
numberTwo.Text = ("You entered an invalid number");
}
You will see that ive used "if" statements to check whether an actual number was entered in the text box, however when i type a letter in that textbox the program still crashes rather than giving the "You entered an invalid number" message.
Any ideas?
Simple bool question
Started by noxrawr, May 14 2010 08:09 AM
5 replies to this topic
#1
Posted 14 May 2010 - 08:09 AM
|
|
|
#2
Posted 14 May 2010 - 09:12 AM
Is this windows forms Program , if yes try this code .
private void calc_Click(object sender,EventArgs e)
{
int input = int.Parse(number.Text);
[COLOR=DarkRed]if (input < 10)[/COLOR]
check.Text = "the number you entered is less than to 10";
[COLOR=DarkRed]else [/COLOR]
{
[COLOR=DarkOrange]if (input == 10)[/COLOR]
check.Text = "the number you entered is equal to 10";
[COLOR=DarkOrange]else[/COLOR]
[COLOR=YellowGreen] if (input > 10)[/COLOR]
check.Text = "the number you entered is larger than 10";
[COLOR=YellowGreen] else[/COLOR]
numberTwo.Text = "you did not enter a valid number";
}
bool invalidNumber = (input >=0);
[COLOR=SeaGreen] if (invalidNumber)[/COLOR]
numberTwo.Text = input.ToString();
[COLOR=SeaGreen] else[/COLOR]
numberTwo.Text = "You entered an invalid number";
}
#3
Posted 14 May 2010 - 09:38 AM
gokuajmes said:
Is this windows forms Program , if yes try this code .
private void calc_Click(object sender,EventArgs e)
{
int input = int.Parse(number.Text);
[COLOR=DarkRed]if (input < 10)[/COLOR]
check.Text = "the number you entered is less than to 10";
[COLOR=DarkRed]else [/COLOR]
{
[COLOR=DarkOrange]if (input == 10)[/COLOR]
check.Text = "the number you entered is equal to 10";
[COLOR=DarkOrange]else[/COLOR]
[COLOR=YellowGreen] if (input > 10)[/COLOR]
check.Text = "the number you entered is larger than 10";
[COLOR=YellowGreen] else[/COLOR]
numberTwo.Text = "you did not enter a valid number";
}
bool invalidNumber = (input >=0);
[COLOR=SeaGreen] if (invalidNumber)[/COLOR]
numberTwo.Text = input.ToString();
[COLOR=SeaGreen] else[/COLOR]
numberTwo.Text = "You entered an invalid number";
}
Yes its a WPF project.
ALso thanks for your reply, however after entering your code i still get a program error (as in it shuts down) when entering an unrecognized character.
Could the problem lie somewhere else?
This is the whole script:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFHello
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ok_Click(object sender, RoutedEventArgs e)
{
result.Text = ("Hello " +userName.Text);
}
private void calc_Click(object sender, RoutedEventArgs e)
{
int input = int.Parse(number.Text);
if (input < 10)
check.Text = "the number you entered is less than to 10";
else
{
if (input == 10)
check.Text = "the number you entered is equal to 10";
else
if (input > 10)
check.Text = "the number you entered is larger than 10";
else
numberTwo.Text = "you did not enter a valid number";
}
bool invalidNumber = (input >= 0);
if (invalidNumber)
numberTwo.Text = input.ToString();
else
numberTwo.Text = "You entered an invalid number";
}
}
}
#4
Posted 14 May 2010 - 10:30 AM
Your program crashes on the following code line:
Change that one line of code to the following:
int input = int.Parse(number.Text);This is because you tell it to parse the value of number.Text as an integer whereas it isn't an integer when you enter something like 'a'.
Change that one line of code to the following:
int input;
if ( !int.TryParse(number.Text, out input) )
{
numberTwo.Text = "you did not enter a valid number";
return;
}
#5
Posted 14 May 2010 - 11:55 AM
That did the trick, cheers.
Edited by noxrawr, 14 May 2010 - 03:35 PM.
#6
Posted 14 May 2010 - 08:41 PM
You never Told me you got a Exception . That is called an Incorrect input Format exception.
The framework tries converting the text into a number but finds that the character entered by you is NOT CONVERTIBLE so it doesn't know what to do ? then crashes with a Log LOL
more Robust solution would be this
The framework tries converting the text into a number but finds that the character entered by you is NOT CONVERTIBLE so it doesn't know what to do ? then crashes with a Log LOL
more Robust solution would be this
int input;
try
{
input = int.Parse(number.Text);
}
catch()
{
MessageBox.Show("Please Enter a Valid Number");
}
cheers you got the answer :D
Edited by gokuajmes, 14 May 2010 - 08:47 PM.
included code


Sign In
Create Account


Back to top









