im execising myself with little apps I make in c#.
because im at a low-level experience and knowldge im trying to assimilate the things i DO know...
so im making an app that the user enters a number and the prog tell it if its odd or even (very simple)..
as youll see in the code below..the odd and even stuff were no problem really..im not that n00b. but the exception thing is what bugs me the most.
what if the user enters nothing? or better yet a string instead of an int?
here is the code:
/****************************************
* convert the user input to an integer *
* for calculation purposes *
****************************************/
int a = Convert.ToInt32(tbinput.Text);
string b = tbinput.Text;
/******************************************
* making an "if" statement the checks if *
* there is a remainder to the number the *
* user has inputted *
******************************************/
if (b.Length < 1)
{
MessageBox.Show("mistake");
}
else if (a % 2 == 0)
{
tbresult.Text = tbinput.Text + " is an even number"; //if number is even show it in textbox
}
else if (a % 2 != 0)
{
tbresult.Text = tbinput.Text + " is an odd number"; //if not, then it is odd. show it in textbox
}
you can see the var called "b".
i made it to use the input.tb.text's length property..
i thought id declare this:
else if (b.text.length == 0)
{
messagebox.show("you must type something");
}
but its not working!!the funny thing is if i declare this:
else if (b.text.length != 0)
{
messagebox.show("bla bla");
}
the messagebox DOES pop..in clueless with what's wrong here :S
and to think in sometime im gonna start thinkin of building more complicated apps :)


Sign In
Create Account


Back to top









